Created
November 20, 2014 04:36
-
-
Save MehulATL/8622a731538de32c8ad4 to your computer and use it in GitHub Desktop.
pn abstraction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('keychain.services.pn', []) | |
.factory('PN', function($rootScope, $q, PubNub, User) { | |
return { | |
publish: function(channel, body) { | |
console.log('Publishing to', channel); | |
var dfd = $q.defer(); | |
body.user_id = User.carrier.id; | |
body.user_name = User.carrier.name; | |
body.driver_name = User.carrier.name; | |
body.driver_phone = User.carrier.phone; | |
body.driver_email = User.carrier.email; | |
body.company_id = User.company.id; | |
body.carrier_company_name = User.company.name; | |
body.carrier_mc = User.company.mcNumber; | |
PubNub.ngPublish({ | |
channel: channel, | |
error: function(err) { | |
console.log('ngPublish error', err); | |
dfd.reject(err); | |
}, | |
callback: function(data) { | |
dfd.resolve(data); | |
}, | |
message: { | |
req: body | |
} | |
}); | |
return dfd.promise; | |
}, | |
subscribe: function(channel, cb) { | |
console.log('Subscribing to', channel); | |
PubNub.ngSubscribe({ | |
channel: channel, | |
error: function(err) { | |
console.log('Subscribe Error for', channel, err); | |
}, | |
callback: function(data) { | |
$rootScope.$evalAsync(function() { | |
cb(data); | |
}); | |
} | |
}); | |
}, | |
history: function(config) { | |
var dfd = $q.defer(); | |
PubNub.jsapi.history({ | |
channel: config.channel, | |
count: config.count, | |
// start: config.time * 10000000, | |
callback: function(data) { | |
dfd.resolve(config.callback(data[0])); | |
} | |
}); | |
return dfd.promise; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment