-
-
Save thomasbabuj/4950525 to your computer and use it in GitHub Desktop.
This file contains 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
// Facebook SDK | |
angular.module('facebook', []). | |
directive('fb', ['$FB', function($FB) { | |
return { | |
restrict: "E", | |
replace: true, | |
template: "<div id='fb-root'></div>", | |
compile: function(tElem, tAttrs) { | |
return { | |
post: function(scope, iElem, iAttrs, controller) { | |
var fbAppId = iAttrs.appId || ''; | |
// Setup the post-load callback | |
window.fbAsyncInit = function() { | |
$FB._init(); | |
if('fbInit' in iAttrs) { | |
iAttrs.fbInit(); | |
} | |
}; | |
(function(d, s, id, fbAppId) { | |
var js, fjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) return; | |
js = d.createElement(s); js.id = id; js.async = true; | |
js.src = "//connect.facebook.net/en_US/all.js#appId=" + fbAppId; | |
fjs.parentNode.insertBefore(js, fjs); | |
}(document, 'script', 'facebook-jssdk', fbAppId)); | |
} | |
} | |
} | |
}; | |
}]). | |
factory('$FB', ['$rootScope', function($rootScope) { | |
var fbLoaded = false; | |
// Our own customisations | |
var _fb = { | |
loaded: fbLoaded, | |
_init: function() { | |
if(window.FB) { | |
// FIXME: Ugly hack to maintain both window.FB | |
// and our AngularJS-wrapped $FB with our customisations | |
angular.extend(window.FB, _fb); | |
angular.extend(_fb, window.FB); | |
// Set the flag | |
_fb.loaded = true; | |
if(!$rootScope.$$phase) { | |
$rootScope.$apply(); | |
} | |
} | |
} | |
} | |
return _fb; | |
}]); |
This file contains 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
<!DOCTYPE html> | |
<html ng-app='app'> | |
<head></head> | |
<body> | |
<fb app-id='123123'></fb> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment