Multiple routers for an Ember app

六月ゝ 毕业季﹏ 提交于 2019-12-24 16:26:05

问题


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.


回答1:


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.




回答2:


You can seperate your functions as blessenm shown above. But in that case you are continuing to keep a dependency to those files. And router.js is continuing to be a resource used by nearly all developers.

In addition to that answer: For each modul, you can generate a "modul-route.js" file exporting that function. Then by using instance-initializers, you can collect all this functions and run Router.map(...); function for all loaded files.



来源:https://stackoverflow.com/questions/30453814/multiple-routers-for-an-ember-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!