Lately I have been working to become more familiar with Backbone.js (if you’re not sure what I’m talking about? go here: http://backbonejs.org/) Backbone is a JavaScript MVC framework that is very useful in building single page bookmarkable state-aware JavaScript applications. Below you will find an example of how easy it is to leverage Backbone ‘Routes’ to achieve a state-aware application.
In this snippet you will find we are using jQuery’s ready to load an ApplicationRouter and ApplicationView. ApplicationRouter extends Backbone.Router which is the source of the state-aware magic. In ApplicationRouter we set the routes and map them to contained methods to execute–pretty simple stuff. Under ApplicationRouter you will find ApplicationView, ApplicationView is basically used to load ApplicationRouter, observe navigation click events, and to tell ApplicationRouter to change routes.
$(function(){varApplicationRouter=Backbone.Router.extend({//map url routes to contained methodsroutes:{"":"home","home":"home","about":"about","contact":"contact"},deselectPills:function(){//deselect all navigation pills$('ul.pills li').removeClass('active');},selectPill:function(pill){//deselect all navigation pillsthis.deselectPills();//select passed navigation pill by selector$(pill).addClass('active');},hidePages:function(){//hide all pages with 'pages' class$('div.pages').hide();},showPage:function(page){//hide all pagesthis.hidePages();//show passed page by selector$(page).show();},home:function(){this.showPage('div#home-page');this.selectPill('li.home-pill');},about:function(){this.showPage('div#about-page');this.selectPill('li.about-pill');},contact:function(){this.showPage('div#contact-page');this.selectPill('li.contact-pill');}});varApplicationView=Backbone.View.extend({//bind view to body element (all views should be bound to DOM elements)el:$('body'),//observe navigation click events and map to contained methodsevents:{'click ul.pills li.home-pill a':'displayHome','click ul.pills li.about-pill a':'displayAbout','click ul.pills li.contact-pill a':'displayContact'},//called on instantiationinitialize:function(){//set dependency on ApplicationRouterthis.router=newApplicationRouter();//call to begin monitoring uri and route changesBackbone.history.start();},displayHome:function(){//update url and pass true to execute route methodthis.router.navigate("home",true);},displayAbout:function(){//update url and pass true to execute route methodthis.router.navigate("about",true);},displayContact:function(){//update url and pass true to execute route methodthis.router.navigate("contact",true);}});//load applicationnewApplicationView();});
As for the HTML… nothing out of the ordinary. Each div represents a page of our single page app.
HTML
123456789
<ulclass="pills"><liclass="home-pill"><a>Home</a></li><liclass="about-pill"><a>About</a></li><liclass="contact-pill"><a>Contact</a></li></ul><divid="home-page"class="pages">Hi I'm the home page!</div><divid="about-page"class="pages">Hi I'm the about page!</div><divid="contact-page"class="pages">Hi I'm the contact page!</div>