Search code examples
javascriptnode.jsember.jsember-cliember-router

Multiple routers for an Ember app


Is it possible to have multiple router.js for an Ember app?

By default one router.js will have

import Ember from 'ember';
import config from '../../config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

export default Router.map(function() {
  this.resource('sampleroute');

});

and other router.js will have

import Ember from 'ember';
import config from '../../config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

export default Router.map(function() {
  this.resource('sampleroute2');

});

All I need is to make Ember application to read my second router(router2.js) which by default reads the default router.js to set up routes.


Solution

  • You can define ur routes in seprate files inside a function and export that function like

    page1.js

    export default function() {
      this.route('page1');
    }
    

    page2.js

    export default function() {
      this.route('page2');
    }
    

    Then import that into your router.js file.

    import page1Routes from 'page1';
    import page2Routes from 'page2';
    
    var Router = Ember.Router.extend({
      location: config.locationType
    });
    
    Router.map(function() {
      page1Routes.call(this);
      page2Routes.call(this);       
    });
    
    export default Router;
    

    You can call Router.map repeatedly to add new routes. So you can import ur other routes the same way.