Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 余烬;这";更改承诺(查找)回调_Javascript_Ember.js_Promise_Rsvp Promise - Fatal编程技术网

Javascript 余烬;这";更改承诺(查找)回调

Javascript 余烬;这";更改承诺(查找)回调,javascript,ember.js,promise,rsvp-promise,Javascript,Ember.js,Promise,Rsvp Promise,当我想在控制器操作中从服务器获取帐户时,例如: account: false, actions: { // When the oAuth popup returns this method will be called fetchUser: function(id) { // Get the account from the server this.store.find('account', id).then(fu

当我想在控制器操作中从服务器获取帐户时,例如:

account: false,

actions: {

    // When the oAuth popup returns this method will be called
    fetchUser: function(id)
    {           
        // Get the account from the server
        this.store.find('account', id).then(function(account)
        {
            this.set('account', account);
        }, function(error)
        {
            console.log('error', error);
        });

    },
}

它抛出一个错误,因为行
this.set('account',account)
上的“this”不再是控制器。现在如何在此promise回调中在控制器上设置“account”?

一种解决方案是使用典型的

var self = this; 
在进入更改
范围的功能之前,使用
self
而不是
从功能中开始

self.set('account', account);

一种解决方案是使用典型的

var self = this; 
在进入更改
范围的功能之前,使用
self
而不是
从功能中开始

self.set('account', account);

这不是EmberJS所做的,这只是Javascript作用域的工作方式

this
关键字的作用域是使用它的函数

这里有一个链接可以帮助你理解这一点

我还建议在Javascript上观看Crockford的视频,他解释了这一点以及您尝试做的工作

这是他的视频链接


这不是在做什么,这只是Javascript作用域的工作方式

this
关键字的作用域是使用它的函数

这里有一个链接可以帮助你理解这一点

我还建议在Javascript上观看Crockford的视频,他解释了这一点以及您尝试做的工作

这是他的视频链接

}

修好了!添加。绑定(此):-)

}


修好了!added.bind(this):-)

此方法不适用于ember对象<代码>var self=此将抛出一个错误。您需要像这样将其设置为对象属性分配。但这并不能使您取得任何进展,因为您无论如何都需要使用“this”来引用它
this.self
这种方法不适用于ember对象<代码>var self=此将抛出一个错误。您需要像这样将其设置为对象属性分配。但这不会让你有任何进展,因为你无论如何都需要用“this”来引用它
this.self