小编Jos*_*hua的帖子

如何在回调函数之外使用FB.api(JS SDK)响应?

我没有登录甚至调用api的任何问题,我只是在api回调之外得到响应的问题.我知道它是异步运行所以我想将它放在一个可以返回响应的函数中.这是我的想法

//What I would like to be able to do
function fbUser(){
   FB.api('/me', function(response){
      //this logs the correct object
      console.log(response);
   });
//How do I get the response out here?
return response;
}
Run Code Online (Sandbox Code Playgroud)

我想在开始时调用/ me api函数,然后将它传递给我的视图对象(我只是使用Backbone Views中的响应)并根据需要进行其他api调用.我目前通过从回调内部调用视图来完成某些工作

//What I am doing now, but I lose the ability to pass anything other than the 
//the current response to this function/View
FB.api('/me', function(response){
     var newView = new facebookView({model: response});
   });
Run Code Online (Sandbox Code Playgroud)

我正在尝试这个,但因为api调用是异步的,所以我遇到了未定义的问题

//What I started with but had async issues
var fbResponse;
FB.api('/me', function(response){ …
Run Code Online (Sandbox Code Playgroud)

javascript facebook facebook-graph-api facebook-javascript-sdk

15
推荐指数
1
解决办法
1万
查看次数