Javascript 正在加载子记录
我试图加载一个帖子的回复列表(一个帖子有很多回复)。这感觉不对,因为我没有用于响应的路由或控制器(我通过posts\U show controller中的函数创建响应)。我打算在Javascript 正在加载子记录,javascript,model-view-controller,ember.js,Javascript,Model View Controller,Ember.js,我试图加载一个帖子的回复列表(一个帖子有很多回复)。这感觉不对,因为我没有用于响应的路由或控制器(我通过posts\U show controller中的函数创建响应)。我打算在posts.show中加载帖子的回复列表 路由器: App.Router.map(function() { this.resource('posts', function() { this.route('new') this.route('edit', { path: '/:post_id/edit' }
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' })
})
});
因此,在my posts.show route中,我想显示该帖子的所有回复列表,因为一篇帖子有许多回复,而一个回复属于一篇帖子:
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模板包含以下内容:
{{#each responses}}
访问该帖子的回复。这条路对吗?我觉得这不是因为我在控制器中为帖子创建了一个响应。在你的帖子把手模板中,你会这样做
{{#each post in controller}}
{{#each response in post.responses}}
{{response.response_content}}
{{/each}}
{{/each}}
只需确保上面模板的控制器是ArrayController