I would like to include the Facebook javascript libraries in an Angular project such that all the facebook API calls (login, logout, etc) are encapsulated inside a service. But because of the async nature of the FB library my code seems overly verbose and I have several calls to $rootScope.apply() which I'm not sure is best practice.
Right now I have something like this:
app.factory('Facebook', function($rootScope, $window, $q){
var FBdefer = $q.defer();
var FBpromise = FBdefer.promise;
$window.fbAsyncInit = function(){
$rootScope.$apply(function(){
FB.init(/* FB init code here*/);
FBdefer.resolve(FB);
}
}
var fb_service_api = {
login: function(){
var deferred = $q.defer();
FBPromise.then(function(FB){
FB.login(function(response){
$rootScope.$apply(
deferred.resolve(response)
);
});
}
return deferred.promise.
}
}
return fb_service_api;
})
Looking for a good design pattern here that fits well with the angular framework.