Last active
August 29, 2015 14:06
-
-
Save luismasuelli/5720ac743dfb51f75ed1 to your computer and use it in GitHub Desktop.
Facebook Service (factory) to defer a call until the FB SDK instance is ready (or execute the deferred calls, or perform an immediate call, when the FB object is ready)
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
(function(){ | |
var FBModule = angular.module('AngularFB', []); | |
var _fb_has_initialized = false; | |
var SDK = function($scope) { | |
this.$scope = $scope; | |
this._initialized = false; | |
this._calls = []; | |
}; | |
SDK.prototype._makeCall = function(toCall) { | |
if (!this._initialized) { | |
this._calls.push(toCall); | |
} else { | |
toCall(); | |
} | |
}; | |
SDK.prototype.isInitialized = function() { | |
return this._initialized; | |
}; | |
SDK.prototype.initialize = function() { | |
angular.forEach(this._calls, function(v) { | |
v(); | |
}, this); | |
this._initialized = true; | |
}; | |
SDK.prototype.wrap = function(call) { | |
var c = this; | |
return (typeof call !== 'function') ? call : function(){ | |
var args = []; | |
angular.forEach(arguments, function(argument) { | |
args.push(c.wrap(argument)); | |
}); | |
console.log(args); | |
c.$scope.$apply(function(){ | |
call.apply(null, args); | |
}); | |
}; | |
}; | |
SDK.prototype.init = function(params) { | |
var c = this; | |
this._makeCall(function(){ | |
FB.init(params); | |
}); | |
}; | |
SDK.prototype.api = function(path, method, params, callback) { | |
var c = this; | |
this._makeCall(function(){ | |
FB.api( | |
c.wrap(path), | |
c.wrap(method), | |
c.wrap(params), | |
c.wrap(callback) | |
); | |
}); | |
}; | |
SDK.prototype.ui = function(params, callback){ | |
var c = this; | |
this._makeCall(function(){ | |
FB.ui( | |
c.wrap(params), | |
c.wrap(callback) | |
); | |
}); | |
}; | |
SDK.prototype.getLoginStatus = function(callback, force) { | |
var c = this; | |
this._makeCall(function(){ | |
FB.getLoginStatus( | |
c.wrap(callback), | |
c.wrap(force) | |
); | |
}); | |
}; | |
SDK.prototype.login = function(callback, options) { | |
var c = this; | |
this._makeCall(function(){ | |
FB.login( | |
c.wrap(callback), | |
c.wrap(options) | |
); | |
}); | |
}; | |
SDK.prototype.logout = function(callback) { | |
var c = this; | |
this._makeCall(function(){ | |
FB.logout( | |
c.wrap(callback) | |
); | |
}); | |
}; | |
SDK.prototype.getAuthResponse = function(callback) { | |
while (!this._initialized) { | |
//noop | |
} | |
return FB.getAuthResponse(); | |
}; | |
SDK.prototype.custom = function() { | |
var c = this; | |
var parameters = [FB]; | |
var call = Array.prototype.shift.apply(arguments); | |
angular.forEach(arguments, function(argument) { | |
parameters.push(c.wrap(argument)); | |
}); | |
this._makeCall(function() { | |
FB[call].apply(parameters); | |
}); | |
}; | |
var _sdk = null; | |
function sdk($rootScope) { | |
if (_sdk == null) { | |
_sdk = new SDK($rootScope); | |
} | |
if (_fb_has_initialized && !_sdk.isInitialized()) { | |
_sdk.initialize() | |
} | |
return _sdk | |
} | |
function initialize() { | |
if (_sdk && !_sdk.isInitialized()) { | |
_sdk.initialize(); | |
} | |
_fb_has_initialized = true; | |
} | |
FBModule.factory('AngularFB.SDK', ['$rootScope', sdk]); | |
FBModule.run(['$window', function($window){ | |
$window.angularFBInit = initialize; | |
}]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment