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>