简体   繁体   中英

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). I intend to load the list of responses for a post in 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:

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:

{{#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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