6

同じサブリソースを持つ2つのリソースがあります。

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

  this.resource('product', function() {
    this.resource('comments', function() {
      this.route('new');
    });
  });
});

問題は、emberルーターが、階層全体からではなく、現在のルートと親ルートだけからルートオブジェクトの名前を作成することです。/posts/:id/comments/newしたがって、とオブジェクトの両方をルーティングしようとし/products/:id/comments/newますApp.NewCommentRoute。これを修正するにはどうすればよいですか?

この投稿は、GitHubの問題を基にしています。

4

3 に答える 3

6

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');
    });
  });
});

これにより、transitionTo('product.comments.new')またはApp.register('route:product.comments.new', myRouteHandler)当初の期待どおりに使用できるようになりました。

ルートハンドラーを手動で登録しない場合、Emberは優雅にそれを検索しますApp.ProductCommentsNewRoute

唯一の欠点は、親リソースがすでに持っているのと同じルート名でサブリソースの名前を定義する冗長性です。

于 2013-02-18T22:00:50.030 に答える
4

ルートを指定すると、パスはデフォルトでルートの名前になりますが、その動作をオーバーライドできます。名前に情報を追加することで、ネストされたルートを明確にすることができます。基本的に同じ結果を達成するには、2つの方法があります。

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

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

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

どちらの場合も、ルーターはとを検索しApp.NewPostCommentsPathますApp.NewProductCommentsPath。最初のルートと2番目のルートの利点は、ルートを外部から参照する場合、「comments.newPost」ではなく「postComments.new」のように見えることです。前者の方が読みやすいです。

于 2013-02-17T17:36:14.930 に答える
1

2年が経過するにつれて、Emberは大幅に改善されました。

Ember 1.7以降、ルートにはサブルートも含めることができます: http ://emberjs.com/blog/2014/08/23/ember-1-7-0-released.html#toc_new-features 。

したがって、これを次のように書き直すことができます。

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');
    });
});
于 2015-03-04T14:48:38.330 に答える