2013-08-07 80 views
2

我已经在我的余烬路线中嵌套资源。让我们假设根据this example,我router.js样子:如何在Ember.js中定义嵌套资源的路由名称?

App.Router.map(function() { 
    this.resource('post', { path: '/post/:post_id' }, function() { 
    this.resource('comment', { path ':comment_id' }); 
    }); 
}); 

的文件说,这应该产生这样的路线:

/post/:post_id    post.index 
/post/:post_id/:comment_id comment.index 

不过,我想这是post.showcomment.show,我该如何重新命名这些路线?

+0

我已经回答了您的问题。我的例子适合你? –

+0

@MárcioRodriguesCorreaJúnior对于我来说,这不是一个解决方法。然而在你的JSbin例子中,评论没有列出,我不能确认它的工作。 – p1100i

+0

是的,它就像一个解决方法,但我没有找到其他方法。你可以通过http://jsbin.com/ucanam/647#/post/1/1访问评论。或者点击Marcio链接http://jsbin.com/ucanam/647#/post/1 –

回答

1

我已经得到这个与以下设置工作。

路由器与动态段:

App.Router.map(function() { 
    this.resource('post.show', { path: '/post/:post_id' }); 
    this.resource('comment.show', { path: '/post/:post_id/:comment_id' }); 
}); 

重写serializeApp.CommentShowRoute以反映预期的网址:

App.CommentShowRoute = Ember.Route.extend({ 
    serialize: function(model) {  
     return {   
      post_id : model.get("post_id"), 
      comment_id : model.get("id") 
     }; 
    } 
}); 

implementating这是URL产生的主要问题,因为在该模型serialize挂钩,未加载,post_id属性不存在,并且在加载模型后,不会自动更新。因此,定位评论的linkTo将有一个网址post/undefined/1,而不是post/1/1。 单击链接工作,因为模型加载一段时间后,但如果用户刷新页面,将无法正常工作,因为URL不正确。

为了解决这个问题,我用this讨论论坛评论中的3种方法,我们用action帮手代替linkTo。唯一的区别是,链接将不具有URL的href`属性,但是当点击所有作品时。

结果是:

App.PostShowRoute = Ember.Route.extend({ 
    events : { 
     goToComment: function(context){ 
      this.transitionTo('comment.show', context); 
     } 
    } 
}); 

这里是一个demo全面实施。