Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Ember.js中定义嵌套资源的路由名称?_Ember.js_Routes - Fatal编程技术网

如何在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
属性不存在,并且在加载模型后不会自动更新。因此,针对评论的
链接将有一个url
post/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链接