Fed*_*d03 7 javascript handlebars.js ember.js ember-data
我需要在hasMany关系中显示第一个项目
基本上一个线程可以有多个作者,但我需要只显示特定模板中的第一个
我有以下的json
{
threads: [
{
id: 1,
authors: [2,3]
}
],
authors: [
{
id: 2,
fullname: "foo"
},
{
id: 3,
fullname: "bar"
}
]
}
Run Code Online (Sandbox Code Playgroud)
以及以下型号
App.Thread = DS.Model.extend({
authors: DS.hasMany('author')
});
App.Author = DS.Model.extend({
fullname: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
现在在我的模板中,我想做一些类似{{thread.authors[0].fullname}}但不起作用的东西.我也尝试过thread.authors.0.fullname根据把手的语法,但没有任何改变.
Thnx提前为您提供帮助
Pan*_*agi 18
使用Ember.Enumerable firstObject:
{{thread.firstObject.fullName}}
Run Code Online (Sandbox Code Playgroud)
如果要在很多地方使用它,最好将其定义为模型中的计算属性:
App.Thread = DS.Model.extend({
authors: DS.hasMany('author')
firstAuthor: Ember.computed.alias('authors.firstObject')
});
Run Code Online (Sandbox Code Playgroud)
并在模板中使用它:
{{firstAuthor.name}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3076 次 |
| 最近记录: |