Skip to content

Commit

Permalink
Refactor login() method to invoke FB.login() synchronously. Fixes issue
Browse files Browse the repository at this point in the history
luiscarlosjayk#24.

Bump Version #
  • Loading branch information
Orr Bibring committed May 16, 2014
1 parent 27c5a72 commit b00a16d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
47 changes: 45 additions & 2 deletions lib/angular-facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,54 @@ provides: [facebook]
return flags.ready;
};

NgFacebook.prototype.login = function () {

var d = $q.defer(),
args = Array.prototype.slice.call(arguments),
userFn,
userFnIndex; // Converts arguments passed into an array

// Get user function and it's index in the arguments array, to replace it with custom function, allowing the usage of promises
angular.forEach(args, function(arg, index) {
if (angular.isFunction(arg)) {
userFn = arg;
userFnIndex = index;
}
});

// Replace user function intended to be passed to the Facebook API with a custom one
// for being able to use promises.
if (angular.isFunction(userFn) && angular.isNumber(userFnIndex)) {
args.splice(userFnIndex, 1, function(response) {
$timeout(function() {
if (angular.isUndefined(response.error)) {
d.resolve(response);
} else {
d.reject(response);
}

if (angular.isFunction(userFn)) {
userFn(response);
}
});
});
}

if (this.isReady()) {
$window.FB.login.apply($window.FB, args);
} else {
$timeout(function() {
d.reject("Facebook.login() called before Facebook SDK has loaded.");
});
}

return d.promise;
};

/**
* Map some asynchronous Facebook sdk methods to NgFacebook
*/
angular.forEach([
'login',
'logout',
'api',
'ui',
Expand Down Expand Up @@ -550,4 +593,4 @@ provides: [facebook]
}
]);

})(window, angular);
})(window, angular);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angularjs-facebook",
"version": "0.2.2",
"version": "0.2.3",
"description": "An AngularJS module to take approach of the Facebook Javascript SDK.",
"main": "lib/angular-facebook.js",
"directories": {
Expand Down

0 comments on commit b00a16d

Please sign in to comment.