【发布时间】:2015-01-10 16:24:34
【问题描述】:
我似乎没有在我的路由器中正确设置我的模型挂钩,因为我的数据正在模板中呈现。但也许我还缺少其他东西
这是我的路由器
Router.map(function() {
this.resource('movies', function() {
this.route('show', { path: ':movie_id'}, function() {
this.resource('rewrites', function() {
this.route('show', { path: ':id'});
this.route('new');
this.route('edit');
});
});
this.route('edit', { path: ':movie_id/edit'});
this.route('new');
});
});
在我下面的重写索引模板模板/重写/index.hbs 中,我需要显示针对一部特定电影的所有重写
{{outlet}}
{{link-to 'Add a New Rewrite' 'rewrites.new'}}
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Script</th>
<th>Author</th>
<th></th>
</tr>
</thead>
<tbody>
{{#each rewrite in movie}}
<tr>
<td>
{{rewrite.name}}
</td>
<td>{{rewrite.script}}</td>
<td>{{rewrite.author}}</td>
<td>{{link-to 'Edit this Rewrite' 'rewrites.edit' this}}</td>
<td><a href="#" {{action 'delete' rewrite}}>Delete</a></td>
</tr>
{{/each}}
</tbody>
</table>
我的路线中的模型设置,routes/rewrites/index.js,是
model: function() {
var movie = this.modelFor('movies.show');
return this.store.findAll('rewrite', { movie:movie });
},
我已经为模型尝试了很多东西,也尝试过
return this.store.findAll('rewrite');
无论哪种方式都找到并渲染了模板,但没有数据 创建重写 2 后,我尝试过
return this.store.find('rewrite', 2);
这也不会呈现任何数据。
更新:
上面的路由器是正确的。上面的templates/rewrites/index.hbs应该是{{#each rewrite in model}}
模型钩子应该是
model: function() {
return this.modelFor('movies.show').get('rewrites');
},
另外,这个应用程序的 api 是一个 rails 应用程序。在电影的 rails 序列化程序中,我需要将 has_many 添加到 sideload 重写中,如下所示:
class MovieSerializer < ActiveModel::Serializer
attributes :id, :title, :director, :releaseDate, :cast, :description, :imageUrl
has_many :rewrites, embed: :ids, include: true
end
【问题讨论】:
标签: ember.js