在facebook api回调的上下文? [英] Context on facebook api callback?
问题描述
function user(id){
this.id = id;
this.getUserName = function(fields,callback){
FB.api({
method:'fql.query',
query:'SELECT'+ fields.toString )+'FROM profile WHERE id ='+ this.id
},
callback
);
}
this.getUserName(['name'],function(response){this.name = response [0] .name;});
}
var amigo = new user('fb_id_here');
编辑:这只是解。 Apply()可以与闭包一起使用,以返回绑定到对象范围的函数(请参阅Jamie的帖子)。
Ex:
function bindScope = function(context,obj)
{
return function()
{
return obj.apply(context);
}
}
我相信你可以使用javascript的apply )。尝试将第8行改为callback.apply(this)。
上下文资源申请 - http://kossovsky.net/index .php / 2009/07 / function-context-and-apply-function /
Is there a way to pass context in a javascript facebook sdk api callback? Here's a simple exemple. Now this won't work because the variable 'this.name' in my callback function would be undefined, because it's not in my user object context. Any idea how to do it?
function user(id) {
this.id = id;
this.getUserName = function(fields,callback){
FB.api({
method:'fql.query',
query: 'SELECT '+ fields.toString() +' FROM profile WHERE id=' + this.id
},
callback
);
}
this.getUserName(['name'],function(response){this.name = response[0].name;});
}
var amigo = new user('fb_id_here');
Edit: This is only part of the solution. Apply() can be used with closures to return a function that is bound to an object's scope (see Jamie's post).
Ex:
function bindScope = function(context, obj)
{
return function()
{
return obj.apply(context);
}
}
I believe you can change the context using javascript's apply(). Try changing line #8 to callback.apply(this).
Resource on context & apply - http://kossovsky.net/index.php/2009/07/function-context-and-apply-function/
这篇关于在facebook api回调的上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!