Ember get current route name

So I’m trying to get the current route name (to make sure it doesn’t equal a certain route to show this section of the layout)… Right now, I’m using something like this:

import Ember from 'ember';

export default Ember.Controller.extend({
  routing: Ember.inject.service('-routing'),

  queryParams: ['fname', 'lname', 'page', 'perPage', 'sort', 'dir', 'q'],
  page: 1,
  fname: '',
  lname: '',
  q: '',

  isSearching: function() {
    var currentRoute = this.get('routing').get('currentRouteName');
    console.log(currentRoute);
    if ((this.get('q') !== '' || this.get('fname') !== '' || this.get('lname') !== '') || currentRoute == 'contracts.index') {
      return true;
    }

    return false;
  }.property('q', 'fname', 'lname')
});

This doesn’t seem to update quick enough when I navigate to a different page. I’m on contracts.index and then I click a link that should lead to contracts.contract.index which it does, but the routing service doesn’t seem to update quick enough… it still says I’m on contracts.index and then if I do a simple page refresh, THEN it shows contracts.contract.index. Is there another way around this?

I do a console.log(this.get('routing')) and it SHOWS that the currentRouteName property DOES equal contracts.contract.index, but whenever I do a .get('currentRouteName') it still says contracts.index which makes no sense… when i do that console log it tells me something else until I perform the .get() on the property.

Thank you for any help!

1 Like

Would this work? Ember.getOwner(this).lookup('controller:application').currentPath;

1 Like

Just tried this. I get the same result!

You’ll need to share this to your controller or a service but if you are in the route it has a routeName property:

import Ember from 'ember';

export default Ember.Route.extend({
    afterModel() {
        console.log(this.get('routeName'));
    }
});

I have put this in my route, and it comes up undefined.

import Ember from 'ember';
import RouteMixin from 'ember-cli-pagination/remote/route-mixin';

export default Ember.Route.extend(RouteMixin, {
	application: Ember.inject.service(),

	page: 1,
        perPage: 15,

	queryParams: {
		sort: { refreshModel: true },
		dir: { refreshModel: true },
		q: { refreshModel: true },
		fname: { refreshModel: true },
		lname: { refreshModel: true },
		page: { refreshModel: true },
		perPage: { refreshModel: true }
	},

	init() {
		this._super();
		console.log('route name');
                console.log(this.get('routeName'));
        }
}

I put this in the setupController method and got it working, but all it returns is contracts as the routeName no matter what sub route I’m in of contracts… that’s not exactly what I need.

you’re right, it wasn’t there in the init hook. Your subroutes would need to implement something similar if you need their names. I simple mixins to help with this.

I was getting the same results in the route as well. This is actually all I needed to do:

isSearching: function() {
  var currentRoute = this.get('routing').get('currentRouteName');
  if ((this.get('q') !== '' || this.get('fname') !== '' || this.get('lname') !== '') || currentRoute === 'contracts.index') {
    return true;
  }

  return false;
}.property('q', 'fname', 'lname', 'routing.currentRouteName')

I wasn’t watching routing.currentRouteName for change. Now it works perfectly! Thank you for the help anyway everybody!

just to keep up to date today is no longer used this.get(‘routing’).get(‘currentRouteName’) the correct one to use today is this.get(‘router’).get(‘currentRouteName’)