I need to get the current route name in my ember application; I tried this: Ember App.Router.router.currentState undefined but it doesn't work for me (there is probablig something i'm missimg...) I use Ember rc6 and I have a multilingual app; in the applicationRoute I detect the browser's language and I redirect to the correct page with:
this.transitionTo(userLang);
but I would like this to be executed only when user are on the home page, so something like this:
if (currentRoute == 'home'){
this.transitionTo(userLang)
}
This worked for me on 1.3.0-beta (and a quick glance at the source for 1.1.2 suggests it would work there too):
Note that the documentation states:
However, I believe it's strongly suggested that
App.__container__
not be used in production code. A more acceptable alternative would be to useApp.Router.router.currentHandlerInfos
, which provides information on the current Ember route.Yet another option is
currentRouteName
on theApplicationController
. You can addneeds: ['application']
to your controller, then access the route name withcontrollers.application.currentRouteName
. This will return something likeposts.index
.With the shift to components, it is harder to get route name. The best way is to add an initializer such as
(from command line), and
in a initializers/router.js. You can also inject into controller if you need to. Then just do simply
in JS, or
in template.
This is the only way I have found to get it reliably, and observable in Ember 2.4
Just as an update, in Ember 1.8.1, we can get the routeName inside an Ember.Route object by doing
this.routeName
.I had the same problem for a while. then i started exploring router. It always have a state object which can be obtained from any route using
that's it. Now you have the current route name!
if you want the current route params,
You can simple parse the current URL. This way you can use your full url for example:
and extract from this string the suffix:
which is the current route name.
A simple JS function (that works regardless to your Ember version) will be:
Example of use:
The Ember namespace API now has a
getOwner
method, which is very useful for looking up thecurrentRouteName
, or, other route properties.I've created an Ember Twiddle example to demonstrate. Use the text input above the "Output" pane to hit other routes like
/blue
,/green
, or/red
.