How are ember links to reused nested routes defined using link-to?

旧时模样 提交于 2019-12-24 12:40:31

问题


Using the example route defined below how can a link be defined to /post/123/comments?

Router.map(function() {
  this.route('post', { path: '/post/:post_id' }, function() {
    this.route('edit');
    this.route('comments', { resetNamespace: true }, function() {
      this.route('new');
    });
  });
  this.route('comments');
});

Since resetNamespace: true is set on comments, the route post.comments does not exist. Otherwise the following would work.

{{#link-to "post.comments" "123"}}Link{{/link-to}}

When trying to link to news using the comment id, the error More context objects were passed than there are dynamic segments for the route: comments occurs.

{{#link-to "comments" "123"}}Link{{/link-to}}

An example of the second case can be seen here: https://ember-twiddle.com/d9f3b5e692573c80e803


回答1:


Since you're resetting the namespace, you are effectively overriding one of the comments route with the other. You're doing something akin to

obj['comments'] = firstRoute;
obj['comments'] = secondRoute;

Route names need to be unique, which they would be if you didn't reset the namespace, as well as the path. If two routes have the /foobar path, then Ember won't know which one to transition to.

If what you want is to reuse parts from other routes, there are ways to do that.




回答2:


Thanks to locks for pointing me in the direction. The specific issue is how to reuse as much of the route and template as possible for different paths.

router.js:

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

routes/post/comments.js:

import Comments from '../comments';

export default Comments.extend({
  renderTemplate() {
    this.render('comments');
  }
});

This will extend the existing comments route to reuse the model and actions defined in the base route. The renderTemplate is still necessary to load the comments template instead of the post.comments template.



来源:https://stackoverflow.com/questions/33723861/how-are-ember-links-to-reused-nested-routes-defined-using-link-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!