0
votes

I have my custom Ember component which is inside a controller.

Now I am trying to get the current route name dynamically

So I get access to the controller using

this.get('parentView')

But I am unable to get the current route name after this. How do I get the current route name ? I tried doing

this.get('parentView').routeName
3

3 Answers

2
votes

In ember 2.15 will be a router service for this. Currently there is a polypill for older ember versions.

Edit: Ember 2.15 has been released on September 1, 2017. You find some information about RouterService in release notes and api documentation. Please note that api docs currently does not mention properties exposed due to missing documentation in code. Additionally to ember-router-service-polyfill you could also access private RoutingService via -routing in earlier releases.

1
votes

First of all, you shouldn't implement a logic according to the current route in a component. Your components should not be aware of the route they are been placed. But of course there may some exceptional cases, such as breadcrump components, menu components...

Anyway, the answers of your question are:

  1. By using application controller:

application controller has a currentRouteName property.

You can use application controller such as:

Ember.getOwner(this).lookup('controller:application').get('currentRouteName')
  1. By using router:

Either inject the private -router service to your component or lookup for the router instance such as:

 Ember.getOwner(this).lookup('router:main').get('currentRouteName');
0
votes

In ember 3.20 I can access the current route name in this way

this.YOURAPP._applicationInstances.values().next().value.router.currentRouteName