Howto share subroutes without duplicating them?


#1

Hi Guys,

How can I re-use subroutes in multiple routes…

For example(comments is the subroute I want to re-use/share):

this-route('/posts/edit/', {path: /posts/:post_id/edit }, function() {
  this.route('comments');
});
this-route('/posts/view', {path: /posts/:post_id/view }, function() {
  this.route('comments');
});
//The following coments view is showing an empty table..
this-route('/posts/new', function() {
  this.route('comments');
});

Do I have to dupplicate the route named: ‘comments.js’ or…?


#2

You have to duplicate them indeed.

To make this easier, I usually have both be one-liner, inheriting from a common file. The drawback is they will be two different routes, so they’ll not share their state, should that matter.


#3

Not sure, but maybe you can register your route via registry.

Edit: Got it. https://ember-twiddle.com/3512d9c176cbd238497a?openFiles=initializers.test-initializer.js%2C :slightly_smiling:

Although, I would go with base comments route and then create comments route as extended base for every top level route.