2013-04-09 78 views
1

我在下面显示的路由结构如下jsfiddle。下山时会显示这个问题,我点击的帖子链接上的应用程序模板所示时,它抛出的错误:emberjs嵌套的路由返回未定义的对象,但对象已定义

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> 

回答

1

您在post资源上定义了动态细分。这意味着如果你想链接到它,你需要通过post模型。

例子:

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

这里的updated fiddle

+0

感谢您的帮助再次。我欠你的不仅仅是感谢,而且已经接受你的回答。欢呼声 – brg 2013-04-10 10:21:22

1

除了什么泰迪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'}); 
     }); 
    }); 
    }); 
}); 

来清理东西。

(对不起,发布这个答案,但代码示例只是太长的评论。)

+0

感谢您的帮助。我欠你的不仅仅是一种感谢。我已经提出了你的答案。 – brg 2013-04-10 13:08:50