【问题标题】:EmberJS loading child recordsEmberJS 加载子记录
【发布时间】:2013-06-21 20:44:58
【问题描述】:

我正在尝试加载帖子的回复列​​表(帖子有很多回复)。感觉不对,因为我没有响应的路由或控制器(我通过 posts_show 控制器中的函数创建响应)。我打算在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' })
})


});

所以在我的 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')
})

我的 posts.show 模板具有以下内容:

{{#each responses}}

访问该帖子的回复。这是正确的方法吗?我觉得这不是因为我正在控制器中为帖子创建响应。

【问题讨论】:

    标签: javascript model-view-controller ember.js


    【解决方案1】:

    在你的帖子车把模板中,你会做这样的事情

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

    只要确保上面模板的控制器是一个 ArrayController

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多