2013-04-09 126 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