Ember.js 如何在初始化器中获取路由器实例

Ember.js 如何在初始化器中获取路由器实例,ember.js,ember-cli,ember-router,Ember.js,Ember Cli,Ember Router,我有一个用例,我想在初始化器中动态注册路由。 因为该应用程序是一个自定义的应用程序,我不知道开发时的路线 当前我创建了一个实例初始值设定项: import Ember from 'ember'; const myTempRouteList = ['home']; // this is retrieved from the backend export function initialize(instance) { let container = instance.container;

我有一个用例,我想在初始化器中动态注册路由。 因为该应用程序是一个自定义的应用程序,我不知道开发时的路线

当前我创建了一个实例初始值设定项:

import Ember from 'ember';
const myTempRouteList = ['home']; // this is retrieved from the backend

export function initialize(instance) {
  let container = instance.container;
  let router = container.lookup('router:main');

  myTempRouteList.forEach(function (name) {
    let routeName = name.dasherize();

    router.map(function(){ // router.map is undefined here
      this.resource(routeName, {path: routeName});
    });
    container.register(`route:${routeName}`, Ember.Route.extend({
    }));

  }, this);
}

export default {
  name: 'register-routes',
  initialize: initialize
};

问题是路由器实例存在,但没有方法
map
。在本文中,它被描述为一种公共方法。我检查过的其他一些方法也存在,f.I.
hasRoute

结果我不得不调用
lookupFactory
方法,而不是容器上的
lookup
方法

export function initialize(instance) {
  let container = instance.container;
  let router = container.lookupFactory('router:main');

  ...
}

适用于使用ember cli(ember>2.0)开发最新ember的用户。这可能会有帮助

//initializer.js

export function initialize(application) {
    var routeNames = [];
    var router = application.__container__.lookupFactory('router:main');
    application.deferReadiness();

   //if you want to have your custom routes on the highest level
    if (routeNames.length > 0) {
        router.map(function() {
            var _this = this;
            routeNames.forEach(function(item,index) {
               _this.route(item);
            });
        });
    }

   //if you want to have your custom routes as a child of another parent route
    if (routeNames.length > 0) {
        router.map(function() {
            this.route('parentRoute', {path:'/'}, function(){
                var _this = this;
                routeNames.forEach(function(item,index) {
                    _this.route(item);
                });
            });
        });
    }

    application.advanceReadiness();
}