1

我在这个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>
4

2 回答 2

1

post您在资源上定义了一个动态段。这意味着如果要链接到它,则需要将post模型与它一起传递。

例子:

{{#each postRecord in model}}
  {{#linkTo "post.index" postRecord}}{{postRecord.title}}{{/linkTo}}
{{/each}}

这是更新的小提琴

于 2013-04-10T08:15:32.257 回答
1

除了 Teddy Zeeny 所说的,我还将路由器更改为

EmBlog.Router.map(function() {
  this.resource("posts", {path: '/posts'}, function(){
    this.route('new');

    this.resource('post', {path: '/:post_id'}, function(){
      this.route('edit', {path: '/edit'});
      this.resource('comments', {path:  '/comments'}, function(){
        this.route('new');
        this.resource('comment', {path: '/:comment_id'}, function() {
          this.route('edit', {path: '/edit'});
        });
      });
    });
  });
});

或者至少

EmBlog.Router.map(function() {
  this.resource("posts", {path: '/posts'}, function(){
    this.route('new');

    this.resource('post', {path: '/:post_id'}, function(){
      this.route('edit', {path: '/edit'});
      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'});
      });
    });
  });
});

收拾东西。

(很抱歉将其发布为答案,但代码示例太长了,无法发表评论。)

于 2013-04-10T08:30:06.310 回答