I needs to apply an "active" class to a bootstrap tab depending on the current route name. The route object contains "routeName" but how to I access this from a controller or component?
7 Answers
Use this this.controllerFor('application').get('currentRouteName');
-
@Huafu It should work in Ember 1. Im not sure about ember 2. I havent worked in ember for some time. But Im thinking it wont since controllers are getting replaced in favor of routable components in ember 2. Im not even sure they have moved in that direction. But it would be great if u could update here if u found an alternative. Nov 28, 2015 at 18:52
-
No, I meant,
controllerFor
is a public method ofEmber.Route
, in Ember 2 it is also a private static method ofEmber
, but I don't think it is a method ofEmber.Component
, even private. In other terms, this would work from a route but not from a component because ofcontrollerFor
not accessible there.– HuafuNov 30, 2015 at 0:45 -
1@Huafu Thanks for clearing up. Makes sense because components should ideally only have access to stuff passed in from the controller or through dependency injection. Nov 30, 2015 at 1:03
-
2This does not work in Ember 2. At least not from a
controller
,this.controllerFor
is no longer defined. I think you can do it but you need to use theregistry
orcontainer
or something like that. Dec 14, 2015 at 9:39 -
@PerLundberg You may need to inject the application controller where u need to use it. emberjs.jsbin.com/vuyuha/2/edit?html,js,output Dec 14, 2015 at 9:51
In fact, you don't need to apply active class by yourself. A link-to
helper will do it for you.
See here:
{{link-to}}
will apply a CSS class name of 'active' when the application's current route matches the supplied routeName. For example, if the application's current route is 'photoGallery.recent' the following use of{{link-to}}
:
{{#link-to 'photoGallery.recent'}}
Great Hamster Photos
{{/link-to}}
will result in
<a href="/hamster-photos/this-week" class="active">
Great Hamster Photos
</a>
In the absolutely desperate case, you can look up the router, or the application controller (which exposes a 'currentRouteName' property) via this.container.lookup("router:main")
or this.container.lookup("controller:application")
from within the component.
If it was a common trend for me, I would make a CurrentRouteService and inject it into my component(s) so that I can mock things more easily in my tests.
There may also be a better answer to come along - but the container.lookup() should knock down your current blocker.
-
3It is returning undefined for me:
this.container.lookup("router:main").get('currentRouteName')
– jaxMay 13, 2015 at 6:23
Since Ember 2.15 you can do this through the public Router service.
router: service(),
myRouteName: computed('router.currentRouteName', function () {
return this.get('router.currentRouteName') + 'some modification';
}
https://www.emberjs.com/api/ember/release/classes/RouterService
Which worked really well for me since I wanted something computed off of the current route. The service exposes currentRouteName, currentURL, location, and rootURL.
currentURL has the query params, but you would need to parse them from the URL.
For Ember 2, from a controller you can try :
appController: Ember.inject.controller('application'),
currentRouteName: Ember.computed.reads('appController.currentRouteName')
Then you can pass it to component.
Try this.
export default Ember.Route.extend({
routeName: null,
beforeModel(transition){
//alert(JSON.stringify(transition.targetName) + 'typeof' + typeof transition.targetName);
this.set('routeName', transition.targetName);
},
model(){
// write your logic here to determine which one to set 'active' or pass the routeName to controller or component
}
`
Using insights from @maharaja-santhir's answer, one can think of setting the routeName property on the target controller to use, e.g., in the target's template. This way there's no need for defining the logic in multiple locations and hence code-reusability. Here's an example of how to accomplish that:
// app/routes/application.js
export default Ember.Route.extend({
...
actions: {
willTransition(transition) {
let targetController = this.controllerFor(transition.targetName);
set(targetController, 'currentRouteName', transition.targetName);
return true;
}
}
});
Defining this willTransition
action in the application route allows for propagating the current route name to anywhere in the application. Note that the target controller will retain the currentRouteName
property setting even after navigating away to another route. This requires manual cleanup, if needed, but it might be acceptable depending on your implementation and use case.
-
wouldn't you want to handle this in
didTransition()
instead? Apr 5, 2018 at 14:20 -
@ctcpip possibly, although not 100% sure but depending on what's acceptable to you. I've done it this way to allow for checking the target route before transitioning as I didn't want to fire a transition back if the target route was not allowed by my app's flow/logic.– ashrafApr 5, 2018 at 20:46
Ember.getOwner(this).lookup(‘controller:application’).currentPath;
Refer: discuss.emberjs.com/t/ember-get-current-route-name/10324