ember.jsember-router

How to get all registered routers in ember (version 2.4.6) by es6


I need a list of all registered routers in ember app. After searching on the Internet, I've found the link below:

can we view all the routes in emberjs aka something similar to what rake routes does in rails

Getting a list of routes in Ember.js

But it does not work for me.

this is App.js

import Ember from 'ember';
import Resolver from './resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';
let App;
Ember.MODEL_FACTORY_INJECTIONS = true;
App = Ember.Application.extend({
  modulePrefix: config.modulePrefix,
  podModulePrefix: config.podModulePrefix,
    Resolver,
    ready: function () {  
    }
});
loadInitializers(App, config.modulePrefix);    
export default App;

this is router.js

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

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

Router.map(function() {    
    this.route('about');
    this.route('login');
});

export default Router;

and this is Application Conroller for get all routers

import Ember from 'ember';
import App from 'ember-app/app';

export default Ember.Controller.extend({
    allRouter: [],

    allRouterCalc: Ember.computed('allRouter', function() {        
        let allRouter = this.get('allRouter');
        if(allRouter.length === 0)
        {
            allRouter = App.Router.router.recognizer.names;
        }        
        return allRouter;
    })
});

but when I called "allRouterCalc" variable, this error This bug occurs: "Cannot read property 'router' of undefined" .

This problem arises because the "Router" in "App.Router" is "undefined".

why "Router" is "undefined" ?


Solution

  • There is one Router for every ember apps. So I think you mean you want list all routes.

    You can list all routes like this :

    let router = Ember.getOwner(this).lookup('router:main');
    let allRoutesList = router.get('router.recognizer.names');
    

    Note: This works with version 2.x