我无法直接执行FB.getLoginStatus()方法。当用户单击操作按钮时,我调用FB.getLoginStatus(),但是返回“Undefined”。当我重新加载页面时,FB.getLoginStatus()可以正常工作。如何在页面中按需使用FB.getLoginStatus()。我附上了我的源代码。
函数_checkFBStatus()在用户单击操作按钮时调用FB.getLoginStatus()。
此点击事件是一切发生的地方。我必须实施一个黑客才能让它发挥作用。一旦FB.login回调执行,我重定向到同一页面并调用FB.getLoginStatus()。我宁愿不使用这个黑客:
$('ul.fb')
.click( function( event )
{
if( _checkFBStatus() )
{
_getFriends();
}else
{
_login();
}
}
);
以下是完整的源代码:
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);
});
答案 0 :(得分:0)
你有没有理由不使用FB.login的回复?它应包含与调用FB.getLoginStatus相同的连接信息。