diff --git a/lib/angular-facebook.js b/lib/angular-facebook.js index 139c9d0..2c5d3c4 100644 --- a/lib/angular-facebook.js +++ b/lib/angular-facebook.js @@ -225,14 +225,6 @@ * @arg {Boolean} _loadSDK (optional, true by default) */ this.init = function(initSettings, _loadSDK) { - // If string is passed, set it as appId - if (angular.isString(initSettings)) { - settings.appId = initSettings; - } - - if(angular.isNumber(initSettings)) { - settings.appId = initSettings.toString(); - } // If object is passed, merge it with app settings if (angular.isObject(initSettings)) { @@ -257,10 +249,36 @@ /** * This is the NgFacebook class to be retrieved on Facebook Service request. */ + function NgFacebook() { - this.appId = settings.appId; + if (typeof settings.appId !== "undefined" && settings.appId !== null) { + this.initAppId(settings.appId) + } } + NgFacebook.prototype.initAppId = function(appId) { + this.appId = $q.when(appId).then(function(data){ + // If string is passed, set it as appId + if (angular.isDefined(data)) { + if (angular.isString(data)) { + return $q.when(data); + } + + if(angular.isNumber(data)) { + return $q.when(data.toString()); + } + + return $q.when(data); + } else { + return $q.when(data); + } + }); + }; + + NgFacebook.prototype.getAppId = function() { + return this.appId; + }; + /** * Ready state method * @return {Boolean} @@ -396,7 +414,7 @@ angular.forEach([ 'subscribe', - 'unsubscribe', + 'unsubscribe' ], function(name) { NgFacebook.prototype[name] = function() { @@ -461,7 +479,8 @@ '$q', '$window', '$timeout', - function($rootScope, $q, $window, $timeout) { + 'Facebook', + function($rootScope, $q, $window, $timeout,Facebook) { // Define global loadDeffered to notify when Service callbacks are safe to use loadDeferred = $q.defer(); @@ -473,42 +492,48 @@ */ $window.fbAsyncInit = function() { // Initialize our Facebook app - $timeout(function() { - if (!settings.appId) { - throw 'Missing appId setting.'; - } - FB.init(settings); - - flags.ready = true; - - /** - * Subscribe to Facebook API events and broadcast through app. - */ - angular.forEach({ - 'auth.login': 'login', - 'auth.logout': 'logout', - 'auth.prompt': 'prompt', - 'auth.sessionChange': 'sessionChange', - 'auth.statusChange': 'statusChange', - 'auth.authResponseChange': 'authResponseChange', - 'xfbml.render': 'xfbmlRender', - 'edge.create': 'like', - 'edge.remove': 'unlike', - 'comment.create': 'comment', - 'comment.remove': 'uncomment' - }, function(mapped, name) { - FB.Event.subscribe(name, function(response) { - $timeout(function() { - $rootScope.$broadcast('Facebook:' + mapped, response); + Facebook.getAppId().then(function(appId){ + var finalSettings = angular.extend(settings,{appId:appId}); + $timeout(function() { + if (!appId) { + throw 'Missing appId setting.'; + } + + FB.init(finalSettings); + + flags.ready = true; + + /** + * Subscribe to Facebook API events and broadcast through app. + */ + angular.forEach({ + 'auth.login': 'login', + 'auth.logout': 'logout', + 'auth.prompt': 'prompt', + 'auth.sessionChange': 'sessionChange', + 'auth.statusChange': 'statusChange', + 'auth.authResponseChange': 'authResponseChange', + 'xfbml.render': 'xfbmlRender', + 'edge.create': 'like', + 'edge.remove': 'unlike', + 'comment.create': 'comment', + 'comment.remove': 'uncomment' + }, function(mapped, name) { + FB.Event.subscribe(name, function(response) { + $timeout(function() { + $rootScope.$broadcast('Facebook:' + mapped, response); + }); }); }); + + // Broadcast Facebook:load event + $rootScope.$broadcast('Facebook:load'); + + loadDeferred.resolve(FB); }); - // Broadcast Facebook:load event - $rootScope.$broadcast('Facebook:load'); - loadDeferred.resolve(FB); }); };