How do I get the current route name?

Assuming my application is bound to window.App

: I can get its router using App.__container__.lookup("router:main")

.

How can I access the current route? Specifically, I want to get its property routeName

.

+2


source to share


1 answer


There is a property currentPath

in it that is a computed alias for the current state of the application. To get the current one routeName

, it is the same as you could do something like this:

App = Ember,Application.create({
  currentPath: ''
});

ApplicationController : Ember.Controller.extend({
  currentPathDidChange: function() {
    App.set('currentPath', this.get('currentPath'));
  }.observes('currentPath')
});

      

and then access it from anywhere:

App.get('currentPath');

      

For details on properties currentPath

see here .



It's also worth mentioning that you can enable the flag LOG_TRANSITIONS

to true so that the name of the current route is logged in your browser console every time the application changes state / route:

App = Ember.Application.create({
  LOG_TRANSITION: true
});

      

Note Use this App.__container__.lookup("router:main")

for debugging purposes only as it is an internal API.

Hope it helps.

+10


source







All Articles