Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add active class only for exact routes and not parent routes in ember js?

I have two routes which are nested like this.

router.js

this.route('profile', function() {
  this.route('edit');
});

and couple of navbar links for these routes like this..

navbar.hbs

{{#link-to 'profile' tagName="li"}}<a href>View Profile</a>{{/link-to}}
{{#link-to 'profile.edit' tagName="li"}}<a href>Edit Profile</a>{{/link-to}}

The link-to helper adds active class to the li tag here. So when I am in profile route, the first link has active class and when I am in profile.edit route, both the links have active class. (apparently because both the routes get activated when profile.edit is visited.)

How can I avoid the parent route link to get the active class when in a child route?

Basically I don't want the first link (to profile) to have active class when in profile.edit route.

like image 972
Ashish Tajane Avatar asked Feb 12 '16 22:02

Ashish Tajane


People also ask

How to generate route in Ember?

Basic Routes. The map() method of your Ember application's router can be invoked to define URL mappings. When calling map() , you should pass a function that will be invoked with the value this set to an object which you can use to create routes.

What is outlet in Ember JS?

Here is the explanation: {{outlet}} -> This will provide a stub/hook/point into which you can render Components(Controller + View). One would use this with the render method of routes. In your case you will likely have a details route which could look like this.

What is Route testing?

A route consistency test takes two different route advertisements to the same destination as input and outputs true if the routes are consistent and outputs false otherwise. Consistency is abstractly defined as follows: If both route announcements are valid then the output is true.

What is a model in Ember?

In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name.


1 Answers

I figured it out if anyone else is facing same issue.

I just changed the link to profile and made it to explicitly profile.index.

navbar.hbs

{{#link-to 'profile.index' tagName="li"}}<a href>View Profile</a>{{/link-to}}
{{#link-to 'profile.edit' tagName="li"}}<a href>Edit Profile</a>{{/link-to}}

This way, When in profile.edit route, the first link does not get the active class.

like image 187
Ashish Tajane Avatar answered Sep 27 '22 19:09

Ashish Tajane