Skip to content

Instantly share code, notes, and snippets.

@benoitboucart
Last active August 29, 2015 13:56
Show Gist options
  • Save benoitboucart/8810217 to your computer and use it in GitHub Desktop.
Save benoitboucart/8810217 to your computer and use it in GitHub Desktop.
FB Connect as a service in Angular JS
// Going mobile with Angular by Ari
// Source: https://speakerdeck.com/auser/going-mobile-with-angular?utm_source=ng-newsletter&utm_campaign=b5794c7e19-AngularJS_Newsletter_2_4_142_3_2014&utm_medium=email&utm_term=0_fa61364f13-b5794c7e19-96511737
angular.module('myApp')
.provider('FBService', function() {
function createScript($document, callback, success) {
var scriptTag = $document.createElement('script');
striptTag.async = true;
striptTag.src = '//conect.facebook.net/en_US/all.js';
scriptTag.onreadystatechange = function () {
if(this.readyState == 'complete') { callback(); }
}
scriptTag.onload = callback;
$document.getElementsByTagName('body')[0].appendChild(scriptTag);
}
this.$get = function($document, $timeout, $q, $window) {
var deferred = $q.defer();
createScript($document[0], function(callback){
FB.init({appID: "02393902390"});
$timeout(function() {
deferred.resolve($window.FB);
});
});
return deferred.promise;
}
})
;
// Using this service
angular.module('myApp')
.factory('Facebook', function($q, FBService) {
return {
getUserProfile: function(){
return FBService.then(function(FB) {
FB.api('/me', function(user){
if (user.error){
return $q.reject(user.error);
} else {
return user;
}
});
});
}};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment