gpt4 book ai didi

ember.js - 如何对 Ember 中的每个记录使用不同的 Controller 来迭代 hasMany 集合

转载 作者:行者123 更新时间:2023-12-02 22:10:18 28 4
gpt4 key购买 nike

我在 Ember 中定义了三个模型,并且全部根据请求返回 json:

App.Comment = DS.Model.extend({
discussion: DS.belongsTo('App.Discussion')
});

App.Discussion = DS.Model.extend({
meeting: DS.belongsTo('App.Meeting'),
comments: DS.hasMany('App.Comment')
});

App.Meeting = DS.Model.extend({
discussions: DS.hasMany('App.Discussion')
});

从 session Controller 中的路由 meet/:id 中,我希望能够迭代讨论集合,为每个实例设置一个新的讨论 Controller 。然后,我需要能够迭代每个讨论评论,再次执行相同的操作。目前,我似乎能够访问关联上定义的属性,但如果我调用计算属性,我会返回错误的结果(例如,本应为 5 的计数却为 0)。我认为问题与 Controller 的上下文有关,因为我似乎无法返回 Controller 内该对象的值。这可能是非常简单的事情,但我就是看不到。我做错了什么?

以下是 Controller :

App.MeetingController = Ember.ObjectController.extend({
needs: ['discussion']
});

App.DiscussionController = Ember.ObjectController.extend({
commentCount: function(){
return this.get('comments.length');
}.property('comments')
});

然后在meeting.hbs模板中:

{{#each discussion in discussions}}
{{ render 'discussion' discussion }}
{{/each}}

还有discussion.hbs模板,这有效:

<div>
{{ comments.length }}
</div>

但这并不:

<div>
{{ commentCount }}
</div>

我哪里出错了?

最佳答案

我认为问题在于您如何定义计算属性,看起来绑定(bind)不起作用。

尝试使用 .property('comments.@each') 代替:

App.DiscussionController = Ember.ObjectController.extend({
commentCount: function(){
return this.get('comments.length');
}.property('comments.@each')
});

2014 年 7 月 10 日更新:您现在可以使用:

App.DiscussionController = Ember.ObjectController.extend({
commentCount: Ember.computed.oneWay('comments.length')
});

关于ember.js - 如何对 Ember 中的每个记录使用不同的 Controller 来迭代 hasMany 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16318718/

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