我需要显示 hasMany 关系中的第一项
基本上一个线程可以有多个作者,但我只需要在特定模板中显示第一个作者
我有以下 json
{
threads: [
{
id: 1,
authors: [2,3]
}
],
authors: [
{
id: 2,
fullname: "foo"
},
{
id: 3,
fullname: "bar"
}
]
}
以及以下型号
App.Thread = DS.Model.extend({
authors: DS.hasMany('author')
});
App.Author = DS.Model.extend({
fullname: DS.attr('string')
});
现在在我的模板中我正在尝试做类似的事情{{thread.authors[0].fullname}}
但它不起作用。我也尝试过thread.authors.0.fullname
根据车把语法,但没有任何改变。
在此先感谢您的帮助
使用 Ember.Enumerable 的firstObject http://emberjs.com/api/classes/Ember.Enumerable.html#property_firstObject:
{{thread.firstObject.fullName}}
如果您要在很多地方使用它,最好将其定义为模型中的计算属性:
App.Thread = DS.Model.extend({
authors: DS.hasMany('author')
firstAuthor: Ember.computed.alias('authors.firstObject')
});
并在模板中使用它:
{{firstAuthor.name}}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)