Javascript 如何消除ember.js中嵌套路由的歧义?

Javascript 如何消除ember.js中嵌套路由的歧义?,javascript,ember.js,ember-router,Javascript,Ember.js,Ember Router,我有两个资源,它们都有相同的子资源: App.Router.map(函数(){ this.resource('post',function(){ 此.resource('comments',function(){ 这条路线(“新”); }); }); 此.resource('product',function(){ 此.resource('comments',function(){ 这条路线(“新”); }); }); }); 问题在于,ember router仅从当前路由和父路由中构建路由对

我有两个资源,它们都有相同的子资源:

App.Router.map(函数(){
this.resource('post',function(){
此.resource('comments',function(){
这条路线(“新”);
});
});
此.resource('product',function(){
此.resource('comments',function(){
这条路线(“新”);
});
});
});
问题在于,ember router仅从当前路由和父路由中构建路由对象的名称,而不是从整个层次结构中构建。因此,它尝试将
/posts/:id/comments/new
/products/:id/comments/new
路由到
App.NewCommentRoute
对象。我能做些什么来解决这个问题


此帖子改编自。

当您指定路由时,路径默认为路由的名称,但您可以覆盖该行为。通过在名称中添加更多信息,可以消除嵌套较深的管线的歧义。有两种方法可以实现基本相同的结果:

App.Router.map(函数(){
this.resource('post',function(){
this.resource('postComments',{path:'/comments'},function(){
这条路线(“新”);
});
});
此.resource('product',function(){
this.resource('productComments',{path:'/comments'},function(){
这条路线(“新”);
});
});
});
App.Router.map(函数(){
this.resource('post',function(){
此.resource('comments',function(){
this.route('newPost',{path:'/new'});
});
});
此.resource('product',function(){
此.resource('comments',function(){
this.route('newPost',{path:'/new'});
});
});
});

在这两种情况下,路由器现在将查找
App.NewPostCommentsPath
App.NewProductCommentsPath
。与第二种方法相比,第一种方法的优点是,如果您想在外部引用路由,它们看起来像“postComments.new”,而不是“comments.newPost”。前者更适合我。

我把詹姆斯·罗森(James A.Rosen)的解决方案向前推进了一步,它就像一个符咒。有点多余,但会让事情变得更直观:

App.Router.map(function() {
  this.resource('post', function() {
    this.resource('post.comments', { path: '/comments' }, function() {
      this.route('new');
    });
  });

  this.resource('product', function() {
    this.resource('product.comments', { path: '/comments' }, function() {
      this.route('new');
    });
  });
});
这现在允许您使用
transition('product.comments.new')
App.register('route:product.comments.new',myRouteHandler)

如果您不手动注册路由处理程序,Ember甚至会优雅地在App.ProductCommentsNewRoute中查找它


唯一的缺点是使用父资源已有的相同根名称定义子资源的名称是冗余的。

两年过去了,Ember有了很大的改进

自Ember 1.7以来,路由还可以具有子例程:

因此,我们可以将其改写为:

this.route('post', function() {
    this.route('comments', { path: '/comments' }, function() {
        this.route('new');
    });
});

this.route('product', function() {
    this.route('comments', { path: '/comments' }, function() {
        this.route('new');
    });
});

这本来是我的问题:感觉有点像我的代表被挖走了。很抱歉。我没看见。我将把我的答案标记为社区维基。(虽然我在问题文本中链接到了您的GitHub问题,但问题不可能是这样的。)谢谢。不过,你的答案都是你的,所以你应该得到所有的代表。只有原来的问题是我的。+1谢谢你。这是一个有效的解决办法。尽管如此,正如GitHub上的原始线程中所提到的,这并不是一个真正适合Ember团队的解决方案。