如何在Ember.js中定义嵌套资源的路由名称?
我的余烬路线中有嵌套的资源。假设我的如何在Ember.js中定义嵌套资源的路由名称?,ember.js,routes,Ember.js,Routes,我的余烬路线中有嵌套的资源。假设我的router.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
router.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.show
和comment.show
,如何重命名这些路由?我已通过以下设置完成此操作
具有动态段的路由器:
App.Router.map(function() {
this.resource('post.show', { path: '/post/:post_id' });
this.resource('comment.show', { path: '/post/:post_id/:comment_id' });
});
覆盖App.CommentShowRoute
的序列化
钩子以反映预期的url:
App.CommentShowRoute = Ember.Route.extend({
serialize: function(model) {
return {
post_id : model.get("post_id"),
comment_id : model.get("id")
};
}
});
实现这一点的主要问题是生成的url,因为serialize
钩子中的模型没有加载,post\u id
属性不存在,并且在加载模型后不会自动更新。因此,针对评论的链接将有一个urlpost/undefined/1
,而不是post/1/1
。
在链接中单击可以工作,因为在一段时间后模型被加载,但是如果用户刷新页面,将无法工作,因为url错误
为了解决这个问题,我使用了论坛评论中的3种方法,在这里我们使用action
助手而不是linkTo
。唯一的区别是,链接将不具有url的href`属性,而是在单击“所有作品”时
结果是:
App.PostShowRoute = Ember.Route.extend({
events : {
goToComment: function(context){
this.transitionTo('comment.show', context);
}
}
});
下面是一个完整实施的示例。我已经回答了您的问题。我的例子适用于你的?@MárcioRodriguesCorreaJúnior这对我来说是一个解决办法,而不是一个解决方案。然而,在你的JSbin示例中,没有列出注释,我无法确认它是否有效。是的,这就像一个解决方法,但我没有找到其他方法。您可以通过访问这些评论。或者点击这里的Marcio链接