1

I have a large number of resources that use exactly the same logic. Each resource has a <resource> route and a <resource>.show route. I've defined BaseRoute, BaseShowRoute, BaseController, BaseShowController and corresponding templates to capture this common logic. I set the appropriate controller/templates on the route objects:

// routes/base.js
import Ember from 'ember';

export default Ember.Route.extend({
  controllerName: 'base',
  templateName: 'base'
});

// routes/base/show.js
import Ember from 'ember';

export default Ember.Route.extend({
  controllerName: 'baseShow',
  templateName: 'baseShow'
});

But in order to use these route prototypes with my resources, I have to have two modules for each resource:

// routes/<resource>.js
import BaseRoute from './base';

export default BaseRoute.extend({
});


// routes/<resource>/show.js
import BaseShowRoute from './base/show'

export default BaseShowRoute.extend({
});

This seems silly. I would like to specify that all of these resources should use BaseRoute and BaseShowRoute without needing to create these modules. It would be reasonable to have this option in Router.map. Something like this:

Router.map(function(){
    this.resource('articles', { extends: BaseRoute }, function() {
      this.route('show', { path: ':article_id', extends: BaseShowRoute })
    });
});

But to my knowledge there is nothing like the extends option I'm using above.

The only documentation I can find for the route and resource methods invoked in Router.map is in the Routing Guide. The only option that you can pass to these methods seems to be path. The ember-cli user guide does say that you can override the base class for all generated routes if you define routes/basic.js, but this is not enough for me-- I need multiple base classes.

How can I get rid of my boilerplate route modules?

Sean Mackesey
  • 10,701
  • 11
  • 40
  • 66

1 Answers1

0

In our application we have exactly the same usecase as you. As far as I know there isn't such API in Router. The boilerplate is needed only for routes, so the controller module is not needed and we define it the same way as you do:

// routes/base/show.js
import Ember from 'ember';

export default Ember.Route.extend({
  controllerName: 'baseShow',
});

The problem is that Ember picks the same instance of Controller for every resources. That is ok for most of our resources, but some need their own state (for example: the form has more than one tab and we want to remember the last active tab for each resource) and then we define the controller and override controllerName:

// routes/user/show.js
import BaseShowRoute from './base/show'

export default BaseShowRoute.extend({
    controllerName: 'user/show',
});

It would be great if Route had a property controllerClass telling Ember: Create instance of this Controller for this Route.

stepanhav
  • 73
  • 1
  • 5
  • Hmm, good to see someone else has similar issues. I'm not crazy! This seems like an oversight by the ember-cli team. It is likely fairly common to want to use the same logic for different resources and ember-cli really makes this unnecessarily difficult. Since this SO question hasn't received much love, I'll put some more thought into this and make a post on the Ember discourse forum. – Sean Mackesey Apr 04 '15 at 17:36