我有两个表,一个是帖子,另一个是评论。我有一个只有帖子列表的页面,如果它包含评论,我需要为帖子显示一个图标。
App.Comment = DS.Model.extend({
user_name: DS.attr('string'),
user_id: DS.attr('number'),
comment: DS.attr('string')
});
App.Post = DS.Model.extend({
user_id: DS.attr('number'),
comments: DS.hasMany('App.Comment',{embedded:true}),
这是一个示例 Handlebars
1. testpost <show icon only if comment not empty>
最佳答案
要实现这一点,您可以创建一个计算属性
,如果它至少设置了firstObject
,它会监听您的评论属性,这意味着有评论,一些东西像这样:
App.Post = DS.Model.extend({
user_id: DS.attr('number'),
comments: DS.hasMany('App.Comment',{embedded:true}),
hasComments: function() {
var commentsLength = this.get('comments.length');
return (commentsLength > 0);
}.property('comments.firstObject')
});
然后在您的模板中添加一个 if
助手来监听 hasComments
计算属性,这将显示 if
之间的内容如果 post.hasComments
返回 true,这将是 (commentsLength > 0)
评估为 true
时的帮助程序:
1. testpost {{#if post.hasComments}}<show icon>{{/if}}
编辑
请在此处查看工作 DEMO .
希望对您有所帮助。
关于ruby-on-rails - 具有一对多关系emberjs的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18045620/