Wednesday, 11 September 2013

Requirejs module called before calling FB.init() error

Requirejs module called before calling FB.init() error

I have two require.js modules. In module A there is a function with an
asynchronous call to Facebook, the function is also attempting to
initialize module B.
Module B will only run properly if it is initialized on return of init.
Otherwise, it will return the following error:
FB.getLoginStatus() called before calling FB.init().
Why is it that the module will only load and run properly on return of init?
Here is the code in module A:
:::Working:::
/*global define */
define(['facebookSDK','getstuff'], function (FB,getstuff) {
'use strict';
var init = function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID,
accessToken = response.authResponse.accessToken;
console.log(uid + ' ' + accessToken);
} else if (response.status === 'not_authorized') {
console.log('User has not authorized this App');
} else {
window.top.location = 'https://www.facebook.com/index.php';
}
});
return getstuff.initialize();
};
return {initialize:init};
});
:::Error:::
/*global define */
define(['facebookSDK','getstuff'], function (FB,getstuff) {
'use strict';
var init = function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID,
accessToken = response.authResponse.accessToken;
console.log(uid + ' ' + accessToken);
} else if (response.status === 'not_authorized') {
console.log('User has not authorized this App');
} else {
window.top.location = 'https://www.facebook.com/index.php';
}
});
getstuff.initialize();
};
return {initialize:init};
});

No comments:

Post a Comment