Facebook Connect FB.getLoginStatus issues ~ JS SDK
I am having trouble executing the FB.getLoginStatus() method directly. When a user clicks the action button, I call the FB.getLoginStatus() however, this returns "Undefined". When I reload the page FB.getLoginStatus() works how it should. How can I get use of the FB.getLoginStatus() on demand with in the page. I have attached my source code.
The Function _checkFBStatus() calls the FB.getLoginStatus() when the user clicks the action button.
This click event is where everything takes place. I had to implement a hack in order to get this to work. Once the FB.login callback executes I redirect to the same page and call FB.getLoginStatus(). I would rather not use this hack:
$('ul.fb')
.click( function( event )
{
if( _checkFBStatus() )
{
_getFriends();
}else
{
_login();
}
}
);
Here is the full souce code:
head.ready(function() {
// --------------------------------------------------------------------
/**
* execute the facebook events
* @desc Facebook events executions with custom callbacks
*/
(function($){
// --------------------------------------------------------------------
/**
* Default vars
* @desc set up the default variables
*/
var
fql = 'SELECT first_name,last_name, pic_small , uid, is_app_user FROM user WHERE uid IN (SELECT uid2 FROM friend where uid1 = {0} ) AND is_app_user=1',
fbOptions =
{
appId: 'xxxxxxxxxxxxxxxx',
status: true,
cookie: true,
xfbml: true
},
e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
window.fbAsyncInit = function()
{
FB.init( fbOptions );
FB
.getLoginStatus( function( response )
{
if( MYNYTE.getURLVars()['success'].length > 0 ) _getFriends();
}
);
};
// --------------------------------------------------------------------
/**
* Click initial action
* @desc Start the facebook process
* @acces public
*/
$('ul.fb')
.click(开发者_如何学运维 function( event )
{
if( _checkFBStatus() )
{
_getFriends();
}else
{
_login();
}
}
);
// --------------------------------------------------------------------
/**
* Check Status
* @desc Check the status of the user
* @acces private
*/
function _checkFBStatus()
{
FB
.getLoginStatus( function( response )
{
if (response.status == 'connected')
{
return true;
}
else
{
return false;
}
}
);
}
// --------------------------------------------------------------------
/**
* Login
* @desc Try to log the user in and then call the getFriends method
* @acces private
*/
function _login()
{
FB
.login( function( response )
{
window.location = '/connections?success=true#connections';
},
{scope: 'email'}
);
}
// --------------------------------------------------------------------
/**
* Get Friends
* @desc Try to get the user's frends that are associated with MyNyte
*/
function _getFriends()
{
FB.api('/me',
function( response )
{
var query = FB.Data.query( fql, response.id );
FB.Data.waitOn([ query ],
function( fbReturn ) {
var ret = _parseFBReturn( fbReturn );
}//end callback
);//end fb JS Wait On
}// end FB api callback
);//end api
}//end getFriends
// --------------------------------------------------------------------
/**
* Parse The return FB
* @desc Try to parse the return facebook query
* @params array
* @acces private
*/
function _parseFBReturn( data )
{
var
i, j = 0,
outerArgLength = data.length,
innerArgLength = 0;
for( i = 0; i <= outerArgLength; i++ )
{
innerArgLength = data[ i ].length;
for( j = 0; j <= innerArgLength; j++ )
{
_writeHTML( data[ i ][ j ] );
}//end inner for
}//end outer for
return true;
}
// --------------------------------------------------------------------
/**
* Write the facebook HTML
* @desc Write the html for the facebook import
* @params array
* @acces private
*/
function _writeHTML( user )
{
$('div#facebook-friends')
.append(
$( '<h3 />')
.text( user.first_name + ' ' + user.last_name )
)
.append(
$( '<img />' )
.attr('src', user.pic_small )
);
}
})(jQuery);
});
Is there any reason you don't use the response from the FB.login? It should contain the same connected info as calling FB.getLoginStatus.
精彩评论