在EmberJS中进行异步操作时无法设置控制器的内容

时间:2021-07-16 00:23:41

I'm getting stuck when I want to update my controller's content after a specific action :

当我想在特定操作后更新控制器的内容时​​,我会陷入困境:

this.set('content', CL.myModel.findALL());

this.set('content',CL.myModel.findALL());

I'm not using ember-data or any of them.

我没有使用余烬数据或其中任何一个。

CL.myModel.findALL returns an Ember promise that returns itself the result in it's callback.

CL.myModel.findALL返回一个Ember promise,它在其回调中返回结果。

Actually, I've done the same thing in my setupController in the router and It works well. Why does it not work in the controller itself ?

实际上,我在路由器的setupController中做了同样的事情并且效果很好。为什么它在控制器本身不起作用?

Thank you for helping.

感谢您的帮助。

2 个解决方案

#1


1  

You should do:

你应该做:

controller = this;

CL.myModle.findALL().then(function(models) {
  controller.set('content', models);      
})

I believe the reason that it works without this in Route#setupController is because, behind the scenes, Ember does something similar to the above code with the result of your model hook (which is typically a promise).

我相信它在Route#setupController中没有它的原因是因为,在幕后,Ember使用模型钩子(通常是一个承诺)的结果做了类似于上面代码的事情。

Another option you could look at is DS.PromiseObject (it's a nice abstraction for promises that wrap a promise and expose it's fulfillment value as content. You'd have to extract it from an ember-data build though.

您可以看到的另一个选项是DS.PromiseObject(它是一个很好的抽象,用于包装承诺并将其履行价值作为内容公开。您必须从ember-data构建中提取它。

#2


0  

Here is an example of my model :

这是我的模型的一个例子:

CL.myModle.reopenClass({
   findAll : function(){
        return CL.Utils.ajaxPromise(r)
        .then(this.findByIdSuccess.bind(this), this.findByIdFailed.bind(this));
   },
   findByIdSuccess: function (data) {
       var negotiation = CL.NegotiationModel.create(data);
       this.setMembers(negotiation);
       this.setParties(negotiation);
       this.setVersions(negotiation);

       return negotiation;
   },
})

Here is my controller :

这是我的控制器:

onRefresh : function(){
    this.set('content',CL.myModle.findAll())
}

As you can see in the model part I call some functions that do some model modification and I get it back findByIdSuccess callback.

正如您在模型部分中看到的,我调用了一些进行模型修改的函数,然后将其返回到findByIdSuccess回调中。

I can't just return the datas I retrieve in the "then" of the promise.

我不能只返回我在promise的“then”中检索的数据。

My question again if it's still not clear : Is there any way to get the promise's data without doing :

如果还不清楚的话我的问题是:有没有办法在不做的情况下获得承诺的数据:

CL.myModle.findALL().then(function(models) {
   controller.set('content', models);      
})

#1


1  

You should do:

你应该做:

controller = this;

CL.myModle.findALL().then(function(models) {
  controller.set('content', models);      
})

I believe the reason that it works without this in Route#setupController is because, behind the scenes, Ember does something similar to the above code with the result of your model hook (which is typically a promise).

我相信它在Route#setupController中没有它的原因是因为,在幕后,Ember使用模型钩子(通常是一个承诺)的结果做了类似于上面代码的事情。

Another option you could look at is DS.PromiseObject (it's a nice abstraction for promises that wrap a promise and expose it's fulfillment value as content. You'd have to extract it from an ember-data build though.

您可以看到的另一个选项是DS.PromiseObject(它是一个很好的抽象,用于包装承诺并将其履行价值作为内容公开。您必须从ember-data构建中提取它。

#2


0  

Here is an example of my model :

这是我的模型的一个例子:

CL.myModle.reopenClass({
   findAll : function(){
        return CL.Utils.ajaxPromise(r)
        .then(this.findByIdSuccess.bind(this), this.findByIdFailed.bind(this));
   },
   findByIdSuccess: function (data) {
       var negotiation = CL.NegotiationModel.create(data);
       this.setMembers(negotiation);
       this.setParties(negotiation);
       this.setVersions(negotiation);

       return negotiation;
   },
})

Here is my controller :

这是我的控制器:

onRefresh : function(){
    this.set('content',CL.myModle.findAll())
}

As you can see in the model part I call some functions that do some model modification and I get it back findByIdSuccess callback.

正如您在模型部分中看到的,我调用了一些进行模型修改的函数,然后将其返回到findByIdSuccess回调中。

I can't just return the datas I retrieve in the "then" of the promise.

我不能只返回我在promise的“then”中检索的数据。

My question again if it's still not clear : Is there any way to get the promise's data without doing :

如果还不清楚的话我的问题是:有没有办法在不做的情况下获得承诺的数据:

CL.myModle.findALL().then(function(models) {
   controller.set('content', models);      
})