代码之家  ›  专栏  ›  技术社区  ›  plehoux

facebook api回调的上下文?

  •  3
  • plehoux  · 技术社区  · 16 年前

    有没有办法在javascript facebook sdk api回调中传递上下文?这里有一个简单的例子。因为变量这个名字,因为它不在我的用户对象上下文中。你知道怎么做吗?

    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');
    
    2 回复  |  直到 16 年前
        1
  •  1
  •   John Himmelman    16 年前

    编辑:这只是解决方案的一部分。Apply()可以与闭包一起使用,以返回绑定到对象作用域的函数(参见Jamie的文章)。

    前任:

    function bindScope = function(context, obj)
    {
        return function()
        {
            return obj.apply(context);
        }
    }
    

    http://kossovsky.net/index.php/2009/07/function-context-and-apply-function/

        2
  •  3
  •   Jamie Wong    16 年前

    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(this_user) {
       return function(response){this_user.name = response[0].name;}
     })(this));
    }
    
    var  amigo = new user('fb_id_here');