gpt4 book ai didi

javascript - 在 handlebars 模板中显示 hasMany ember 关系中的第一项

转载 作者:数据小太阳 更新时间:2023-10-29 04:26:17 25 4
gpt4 key购买 nike

我需要显示 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}} 的事情,但它不起作用。我也根据 handlebars 语法尝试了 thread.authors.0.fullname 但没有任何改变。

提前感谢您的帮助

最佳答案

使用 Ember.Enumerable 的 firstObject :

{{thread.firstObject.fullName}}

如果你打算在很多地方使用它,最好将它定义为模型中的计算属性:

App.Thread = DS.Model.extend({
authors: DS.hasMany('author')

firstAuthor: Ember.computed.alias('authors.firstObject')
});

并在您的模板中将其用作:

{{firstAuthor.name}}

关于javascript - 在 handlebars 模板中显示 hasMany ember 关系中的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795210/

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