mardi 31 mars 2015

How to define a large number of resources that use the same logic without creating boilerplate modules in ember-cli?

I have a large number of resources that use exactly the same logic for their routes, controllers, and templates. 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 classes with my resources, I find I have to have two files 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 somehow specify that all of these resources should use BaseRoute and BaseShowRoute without needing to create identical separate modules for each one. It would seem logical to be able to do this 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?





Aucun commentaire:

Enregistrer un commentaire