我在这个jsfiddle中有如下所示的路由结构。当显示此问题下方显示的应用程序模板并单击帖子链接时,它会引发错误:
Uncaught Error: assertion failed: Cannot call get with 'id' on an undefined object.
此错误是由“发布”资源引起的,当我将其更改为下面的代码时,一切都会运行
this.resource('post', function(){});
但我希望它采用如下所示的形式,它会抛出错误,未定义的对象:
this.resource('post', {path: 'posts/:post_id/'}, function(){})
这是整个路由器和jsfiddle
EmBlog.Router.map(function() {
this.resource("posts", {path: '/posts'}, function(){
this.route('new');
this.route('edit', {path: '/:post_id/edit'});
this.resource('post', {path: 'posts/:post_id/'}, function(){
this.resource('comments', {path: '/comments'}, function(){
this.route('new');
this.route('edit', {path: ':post_id/comment/:comment_id'});
this.route('comment', {path: ':post_id/comment'});
});
});
});
});
在我运行 EmBlog.Router.router.recognizer.names 的控制台中
Object {post.index: Object, posts.new: Object, posts.edit: Object, comments.new: Object, comments.edit: Object, comments.comment: Object…}
这是车把模板
<script type="text/x-handlebars" data-template-name="application">
<h1>Hello from Application template</h1>
<p>{{#linkTo posts.index}}Posts{{/linkTo}}</p>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="posts">
<h1>Hello from posts template</h1>
<p>{{#linkTo post.index}} MyPost{{/linkTo}}</p>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="post">
<h1> Hello from a single post template</h1>
<p>{{#linkTo comments.index}} comments{{/linkTo}}</p>
{{outlet}}
</script>