【发布时间】:2014-04-01 01:50:12
【问题描述】:
我正在尝试使用基于 async、hasMany 模型属性的值的计算属性,但无法让它显示在我的视图中。
MyApp.Foo = DS.Model.extend({
title: DS.attr('string'),
peeps: DS.hasMany('peep', { async: true });
});
MyApp.Peep = DS.Model.extend({
name: DS.attr('string'),
email: DS.attr('string')
});
MyApp.Foo.FIXTURES = [
{ id: 1, title: 'nice', peeps: [1,2] }
];
MyApp.Peep.FIXTURES = [
{ id: 1, name: 'mypeep', email: 'peep@example.com' },
{ id: 2, name: 'mypeep2', email: 'peep2@example.com' }
];
MyApp.FooController = EmberObjectController.extend({
showPeeps: function() {
// This one works for this test data.
// return [{name: 'baz', email: 'bar'}];
var peepList = this.get('content.peeps.[]').then(function(c) {
// This one does not work, even for this test data.
return {name: 'baz', email: 'bar'}];
});
}.property('content.peeps.[]');
});
在我看来,大致如下:
{#each peep in controller.showPeeps}}{{peep.name}}{{/each}}
我可以使用 console.log() 查看“then()”中的所有数据,正如代码 cmets 中所指出的,如果我从“then()”中取出返回值,它就可以工作 - 但随后真实数据是空的,因为它是异步返回的。如果我尝试使其非异步,我会得到 p>
未捕获的类型错误:无法调用未定义的“解决”方法
我尝试了许多计算属性代码的变体(使用@each,使用model.peeps - 所有这些都正确显示console.log()中的数据,但不在视图中。在视图中,它是总是未定义,除非我只是在 then() 之外返回虚拟数据 - 显示正确)
我错过了什么?
【问题讨论】:
标签: asynchronous ember.js