Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial work done on the deferred initialization of angular-facebook #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 66 additions & 41 deletions lib/angular-facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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}
Expand Down Expand Up @@ -396,7 +414,7 @@

angular.forEach([
'subscribe',
'unsubscribe',
'unsubscribe'
], function(name) {

NgFacebook.prototype[name] = function() {
Expand Down Expand Up @@ -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();

Expand All @@ -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);
});
};

Expand Down