简体   繁体   English

EmberJS 加载子记录

[英]EmberJS loading child records

I am trying to load a list of responses for a post (a post hasMany responses).我正在尝试加载帖子的回复列​​表(帖子有许多回复)。 It does not feel right because I do not have routes or controllers for responses(I create responses via a function in posts_show controller).感觉不对,因为我没有用于响应的路由或控制器(我通过 posts_show 控制器中的函数创建响应)。 I intend to load the list of responses for a post in posts.show .我打算在posts.show加载帖子的回复列​​表。

Router:路由器:

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' })
})


});

So in my posts.show route, I want to show a list of all responses for that post because a post hasMany responses and a response belongsTo a post:所以在我的 posts.show 路由中,我想显示该帖子的所有回复的列表,因为一个帖子有许多回复,而一个回复属于一个帖子:

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')
})

My posts.show template has the following:我的 posts.show 模板具有以下内容:

{{#each responses}}

To access the responses of that post.访问该帖子的回复。 Is this the right way?这是正确的方法吗? I feel it isn't because I am creating a response in the controller for a post.我觉得这不是因为我正在控制器中为帖子创建响应。

In your posts handlebars template you would do something like this在你的帖子把手模板中,你会做这样的事情

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

Just make sure the controller for the template above is an ArrayController只要确保上面模板的控制器是一个 ArrayController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM