gpt4 book ai didi

javascript - EmberJS 在 Controller 中的 promise

转载 作者:行者123 更新时间:2023-11-28 08:42:56 27 4
gpt4 key购买 nike

我花了几个小时试图找出为什么这不起作用,但我似乎找不到解决方案。

我正在尝试通过 REST 获取数据,对数据执行一些操作(例如过滤数据),然后返回结果。

如果我只返回 this.store.find('somthing'),则效果非常好。一旦我使用 then() - 一切都会崩溃。

App.SongController = Ember.ObjectController.extend({

first: (function() {
return this.second();
}).property('first', '').volatile()


second: (function() {
var promise = Ember.Deferred.create();
var data = [{ id: 1, name: 'hi' }];

this.store.find('something').then(function (data) {
// Do something with the data..

// return the data
promise.resolve(data);
});

return promise;
}).property('second')

});

控制台中的错误:

Assertion failed: The value that #each loops over must be an Array. You passed <Ember.Deferred:ember350>
Uncaught TypeError: Object [object Object] has no method 'addArrayObserver'
Assertion failed: Emptying a view in the inBuffer state is not allowed and should not happen under normal circumstances. Most likely there is a bug in your application. This may be due to excessive property change notifications.
Uncaught Error: You cannot modify child views while in the inBuffer state

最佳答案

听起来您在歌曲模板中使用了 {{#each ...}} ,它告诉 ember 您正在迭代某个数组,但它找到的不是数组。

您可以将 SongController 设置为 ArrayController

App.SongController = Ember.ArrayController.extend()

并从模型钩子(Hook)返回数据

App.SongRoute = Em.Route.extend({
model: function(){
return this.store.find('something');
},
setupController: function(controller, model){
// and if your need to modify the data, it'll be loaded by this point
model.blah = 123123;
// then let ember finish off setting up the controller
this._super(controller, model);
}
});

如果你真的希望它成为一个计算属性,只需从查找中返回 promise ,它是一个数组代理,一旦解析就会填充(因此它最初为 0,但一旦服务器响应它就会增长)。

second: function() {
var promise = this.store.find('something').then(function (records) {
records.objectAt(0).set('someValue', false);
});

return promise;
}.property('second')

关于javascript - EmberJS 在 Controller 中的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20289854/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com