lundi 27 juillet 2015

How to get the Router instance in initializer

I have a use-case where I want to register routes dynamically in an initializer. Because the application is a self-defining app I don't know the routes at development time.

Currently I created an instance-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
};

The problem is that the router instance is present but is has no method map. In the documentation it is described as a public method. Some other methods I checked are present, f.i. hasRoute.




Aucun commentaire:

Enregistrer un commentaire