EmberJS加载子记录

EmberJS loading child records

本文关键字:记录 加载 EmberJS      更新时间:2023-09-26

我正在尝试加载一个帖子的响应列表(一个帖子有许多响应)。这感觉不对,因为我没有响应的路由或控制器(我通过posts_show controller中的函数创建响应)。我打算加载posts.show中一个帖子的回复列表。

路由器:

App.Router.map(function() {
this.resource('posts', function() {
    this.route('new')
    this.route('edit', { path: '/:post_id/edit' })
    this.route('show', { path: '/:post_id' })
}),
this.resource('users', function() {
    this.route('show', { path: '/:user_id' })
})

});

所以在我的帖子里。show route,我想要显示该帖子的所有响应的列表,因为一个帖子有许多响应和一个响应belongsTo a post:

App.Post = DS.Model.extend({
 title: DS.attr('string'),
 post_content: DS.attr('string'),
 author: DS.attr('string'),
 responses: DS.hasMany('App.Response'),
 updated_at: DS.attr('date'),
 announcement: DS.attr('boolean')
});
App.Response = DS.Model.extend({
 response_content: DS.attr('string'),
 response_author: DS.attr('string'),
 post: DS.belongsTo('App.Post')
})

我的帖子。显示模板有以下内容:

{{#each responses}}

访问该帖子的回复。这条路对吗?我觉得这不是因为我在控制器中为一个帖子创建响应。

在你的帖子把手模板中,你可以这样做

{{#each post in controller}}
  {{#each response in post.responses}}
    {{response.response_content}}
  {{/each}}
{{/each}}

确保上面模板的控制器是ArrayController