Search code examples
javascriptember.jsember-cliecmascript-6

Ember-cli : define routes outside of the app/routes/ folder


I am currently writing a big Ember.js application. I know Ember.js is quite monolithic but I wanted to drift a bit from this and create modules that I would plug in my application.

I have no problem with component-only modules or those kind of things. My big issue is to import new routes in my Ember-cli application.

Usually I would have my App.Router and I could create App.xxxRoute whether I define the route in app/routes/my-route.js or in lib/myModule/routes/my-route.js.

With the Ember-cli ES6 Module syntax, the routes taken into account are only the ones from the app/routes folder and as there is no global scope anymore, I can't attach the routes coming from my module to my main application. This is a bit of a pain.

I have tried a lot of things, ranging from trying to add my routes to the Ember.Router.router.recognizer manually (does not work) to creating the routes files in app/routes and exporting the ones from my modules for each of one of them (but it creates a lot of useless files).

The easiest solution would be to be able to add a directory to the ember-cli path loader or whatever it's called but I'm open to any suggestion.

Thanks a lot.


Solution

  • The approach that I would take is to build an add-on, as mentioned in the comments.

    And maybe, depending on your needs, add some initializers that inject objects and anything you like to all your routes or controllers in your app.

    See: Dependency Injection & Service Lookup

    For an example of an add-on that uses initializers you can check Ember Simple Auth