My questions are below, after the code:
var CustomUtils = {
_constPageID: 12345,
// This is called with the results from from FB.getLoginStatus().
_statusChangeCallback: function(response) {
console.log('statusChangeCallback');
console.log(response);
var callback = function(data) {
alert('callback: ' + data); //actually I want the data here to be the return value of my main function below (at the end)
};
if (response.status === 'connected') {
// Logged into your app and Facebook.
CustomUtils._getToken(callback);
}
},
/* main function to get a token */
getAndReturnToken: function() {
$.ajaxSetup({ cache: true });
$.getScript('https://connect.facebook.net/en_US/sdk.js', function(){
FB.init({
appId: '....appid......',
autoLogAppEvents : true,
xfbml : true,
version: 'v3.0'
});
FB.getLoginStatus(CustomUtils._statusChangeCallback);
});
},
};
For this code:
I get a
TypeError: this._statusChangeCallback is not a function[Learn More]
in the console if I try to replaceCustomUtils
withthis
in the above code. UsingCustomUtils
works - why can't I usethis
? Note thatthis._constPageID
can be used just fine (I haven't pasted all my code in this object but it works). It's just the functions within this object that cannot refer to each other usingthis
, it seems.The
getAndReturnToken
function is actually supposed to return a token value, after certain API requests are completed (which actually return a token). The requests are asynchronous, and my newbie questions around this are answered here: How to return value from an asynchronous callback function? However, I don't want to "do more work" in my call back...
I simply want to return the token so it can be used elsewhere. The problem is the API request is asynchronous and we don't know when it will return (by logic), but without a token my code can't do much else as it still needs to use the API for further requests, with that token. Should I use some sort of event listener - or populate the results in my callback function into a variable, and if that variable has a value then return it? Sorry, I'm confused on the 'normal' way to go forward here.