如何在初始化器中获取 Router 实例
How to get the Router instance in initializer
我有一个用例,我想在初始化程序中动态注册路由。
因为该应用程序是一个自定义应用程序,所以我在开发时不知道路由。
目前我创建了一个实例初始化器:
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
。在 documentation 中,它被描述为 public 方法。我检查过的一些其他方法存在,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();
}
我有一个用例,我想在初始化程序中动态注册路由。 因为该应用程序是一个自定义应用程序,所以我在开发时不知道路由。
目前我创建了一个实例初始化器:
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
。在 documentation 中,它被描述为 public 方法。我检查过的一些其他方法存在,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();
}