facebook api - 失去我的范围
facebook api - losing my scope
我正在使用 facebook API 开发应用程序。
我正在尝试的很简单 - 得到一个 post,然后得到这个 post 有多少个赞。
那我想把post故事和它的点赞数结合起来。
稍后我会想用一个循环来完成它 - 迭代一系列 posts.
我的问题解释在代码里面:
$(function () {
$("#mybutton").click(function () {
FB.api("/me/posts", function (response) {
/* first callback
*
* i have access to global scope here.
*/
var firstpost = response.data[0];
FB.api("/" + firstpost.id + "/likes", function (response) {
/* second callback inside the first callback
*
* here i lost my access to the first callback scope.
* my access is only to local and global scope.
* i can't see anymore what is inside firstpost variable;
*/
console.log(firstpost);
//this will give a reference error
})
})
})
});
也许有更好的方法?
您可以只对响应参数使用不同的名称,但有一种更好的方法,即只使用一个 API 调用:
/me/posts?fields=message,likes
...或使用 JS SDK:
FB.api('/me/posts', {fields: 'message,likes'}, function (response) { ... }
我正在使用 facebook API 开发应用程序。 我正在尝试的很简单 - 得到一个 post,然后得到这个 post 有多少个赞。
那我想把post故事和它的点赞数结合起来。 稍后我会想用一个循环来完成它 - 迭代一系列 posts.
我的问题解释在代码里面:
$(function () {
$("#mybutton").click(function () {
FB.api("/me/posts", function (response) {
/* first callback
*
* i have access to global scope here.
*/
var firstpost = response.data[0];
FB.api("/" + firstpost.id + "/likes", function (response) {
/* second callback inside the first callback
*
* here i lost my access to the first callback scope.
* my access is only to local and global scope.
* i can't see anymore what is inside firstpost variable;
*/
console.log(firstpost);
//this will give a reference error
})
})
})
});
也许有更好的方法?
您可以只对响应参数使用不同的名称,但有一种更好的方法,即只使用一个 API 调用:
/me/posts?fields=message,likes
...或使用 JS SDK:
FB.api('/me/posts', {fields: 'message,likes'}, function (response) { ... }