Skip to content

Instantly share code, notes, and snippets.

@deanq
Forked from ruiwen/fb-angular.js
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deanq/11274826 to your computer and use it in GitHub Desktop.
Save deanq/11274826 to your computer and use it in GitHub Desktop.
// Source: https://gist.github.com/ruiwen/4722499
// Modified: https://gist.github.com/deanq/11274826
// 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 || '';
var fb_params = {
appId: "",
cookie: true,
status: true,
xfbml: true
};
if (iAttrs.appId) fb_params.appId = iAttrs.appId;
if (iAttrs.cookie) fb_params.cookie = scope.$eval(iAttrs.cookie);
if (iAttrs.status) fb_params.status = scope.$eval(iAttrs.status);
if (iAttrs.xfbml) fb_params.xfbml = scope.$eval(iAttrs.xfbml);
var initializer = eval(iAttrs.initializer) || null;
// Setup the post-load callback
window.fbAsyncInit = function() {
$FB._init(fb_params, initializer);
if('fbInit' in iAttrs) {
iAttrs.fbInit();
}
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "http://connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}
};
}
};
}])
.factory('$FB', ['$rootScope', function($rootScope) {
var fbLoaded = false;
// Our own customisations
var _fb = {
loaded: fbLoaded,
_init: function(params, initializer) {
var fbNode = (ionic.Platform.isCordova()) ? facebookConnectPlugin : window.FB;
// FIXME: Ugly hack to maintain both window.FB
// and our AngularJS-wrapped $FB with our customisations
angular.extend(fbNode, _fb);
angular.extend(_fb, fbNode);
// Set the flag
_fb.loaded = true;
// Initialise FB SDK
if (initializer) {
initializer.apply(null, [params]);
} else {
fbNode.init(params);
}
if(!$rootScope.$$phase) {
$rootScope.$apply();
}
}
};
return _fb;
}]);
<!DOCTYPE html>
<html ng-app='app'>
<head></head>
<body>
<!-- normal example -->
<fb app-id='123123' cookie="false"></fb>
<!-- using Parse -->
<fb app-id='123123' initializer="Parse.FacebookUtils.init" status="false"></fb>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment