EDIT: this question is very similar to this one but it’s more “conceptual” one.
Assume a single-page app with a bounce of static links that reflects routes:
<!-- Top navlist -->
<ul class="nav nav-list">
<li><a class="active" href="#/customers">Customers</a></li>
</ul>
<!-- Left navlist -->
<ul class="nav nav-list">
<li><a class="active" href="#/customers">Show customers</a></li>
<li><a href="#/customers/new">Create customer</a></li>
</ul>
I need to set class="active" for one (or more) links based on the current route. Which is the correct approach?
- Create
LinkandLinkCollectionmodels, as long asLinkCollectionViewandLinkView, from scratch: this seems overkill to me. And seems useless since links not depend upon server side (they are not dynamically created). - Create
LinkandLinkCollectionmodels iterating over existing links. - Forget about models and view and manually set
class="active"for each route. Something like$('body').find('a[href="#/customers"]').addClass('active'). Seems a duplication of code to me. - Create a “global”
AppViewview and do something like pt. 3 inrender()funcion.
Routes definition example:
<script>
(function($){
var MyApp = { Models : {}, Collections : {}, Views : {} };
// The Router
MyApp.Router = Backbone.Router.extend({
routes : {
'/customers' : 'showCustomersView',
'/customers/new' : 'showCreateCustomerView'
},
showCustomersView : function() {
// Make customers links active
},
showCreateCustomerView : function() {
// Make new customer links active
},
});
})(jQuery);
</script>
I can’t speak to the “best practice”, but I am fairly certain that involving models solely for the purpose of representing intra-app links is, as you said, definitely overkill and not their intended use.
I personally would do something like pattern #4. In a project I am currently working on, I have an AppView that looks a little something like this:
I do not use the actual
hrefproperty on my anchor tags to specify the target route, as I am using pushState. Instead, I use an element class,internal, to indicate an anchor tag which is to be used for my app’s internal navigation. I then use another arbitrary element attribute,route, to indicate where the link ought to be going.A link might look like something like this, then:
<a href="#" class="internal" data-route="projects/3">My Third Project</a>Adding in the extra bit to add that CSS class you want would then just be a matter of making a jQuery/Zepto call in
handleLink.Again, I don’t necessarily believe this is a common/best practice, but at least for my purposes it seems to be sufficiently simplistic and straightforward (and also works with pushState).
Edit: Use steveax’s suggestion of
data-routefor syntax validity sake.