帮助调试路由器问题的一种好方法是打开路由器日志记录:
window.App = Em.Application.create({
LOG_TRANSITIONS: true
});
这可以让您看到您正在转换到哪条路线。在您的情况下,当导航到评论部分时,它会显示:
Transitioned into 'posts.index'
Transitioned into 'posts.post.show'
Transitioned into 'posts.post.comments'
这意味着您的post.show
当您转换到comments
路线,这就是为什么你不能渲染到它的{{outlet}}
.
由于您没有使用post
任何实际内容的模板,您可以重命名post/show
模板到post
并摆脱show
路线。这可以确保当您想要插入时模板仍然存在comment
or trackback
模板。
App.Router.map(function () {
this.resource('posts', function () {
this.resource('post', {
'path': '/:post_id'
}, function () {
this.resource('comments');
this.resource('trackbacks');
});
});
});
由于您将模板更改为post/show
to post
the postController
将包含从以下位置传递的帖子posts
,这样你就可以删除controllers.post
并直接访问帖子的属性:
<script type="text/x-handlebars" data-template-name="post">
...
<h3>{{title}}</h3>
<p>{{description}}</p>
....
</script>
现在您所要做的就是更新要渲染的目标路线,现在只是post
:
renderTemplate: function () {
this.render({
into: 'post'
});
}
工作 JSFiddle http://jsfiddle.net/SM5VM/21/
Update
您可以拥有show
将渲染路由到posts
路线将取代post
模板与你的post/edit
模板:
更新了 JSFiddle http://jsfiddle.net/SM5VM/25/
App.Router.map(function () {
this.resource('posts', function () {
this.resource('post', {
'path': '/:post_id'
}, function () {
this.route('edit');
this.resource('comments');
this.resource('trackbacks');
});
});
});
App.PostEditRoute = Ember.Route.extend({
renderTemplate: function() {
this.render({into: 'posts'});
}
});