Notice that BackboneJS is not a framework but a library. The difference is who is in control. Using a library YOU are in control, but using a framework there is an inversion of control: the framework calls you. Libraries give you a lot of flexibility, while frameworks has opinionated ways of doing things but can save you writing boiler plate code. Follow my AngularJS tutorial series for a framework solution.
A common problem with large JS web application developed is that they can become pretty messy really quickly. The lack of structure makes the code hard to maintain. This is where Backbone comes into play. It provides structure to organize the code and increase maintainability. Backbone is not the only framework like this; in fact, there are many JS frameworks that attempt to offer similar benefits, like Ember.js, Angular.js and so on. However, I choose Backbone because it’s one of the most widely used frameworks in its category. It has a vibrant community and it’s also being fully used in production for a considerable number of big companies like: Wal-Mart mobile, Groupon, Khan Academy, Pandora, Wordpress, Foursquare, and so on.
Just enough to get started with Backbone.js
Backbone.js has hard dependency on underscore.js and a soft dependency on jQuery. It’s made up of the following modules:
Shut up and show me the code!
Alright! the way we are going to explore all of these modules is through examples. This is a practical tutorial that I wished I had it when I stared learning. This is a fat-free walkthrough of Backbone.js, as simple as possible, with all the code in one file for didactical purposes (no hidden magic tricks, all cards are on the table).
The first example is a ‘Hello World’ app in Backbone and the second is a ‘to do’ app. After working through these two example apps, you’ll see every Backbone module and have a practical understanding about them.
You can follow along with this tutorial’s code in this repository. Each feature implemented is a new commit, so you can easily see what changed in every step.
Simple HTML5 and Backbone boilerplate
To get started, download this simple html file. This file contains the libraries that you’ll need (jQuery, Underscore.js, Backbone.js and Backbone-localStorage.js) and the placeholders for your HTML and JS code. Don’t worry about the libraries, we are going to explain them as we need them.
After downloading the aformentioned file, notice the HTML where your entire page will be built using Backbone.Views!
Your entire js app will be loaded here:
Backbone’s Views are the equivalent of ‘controllers’ on MVC frameworks (like Ruby on Rails), if you are not familiar with MVC frameworks, that’s okay. Backbone’s Views glue together user events (clicks, pressed keys, etc.), render HTML views and templates, and interact with models which contains the data of the application.
After copying the code, open the file, refresh the browser and you should see the ‘Hello World’ message, right? Wait, if you’re just seeing the ‘Loading…’ it’s because you need to initialize the view first.
var appView = new AppView();
Yay! You have your “Hello Wold” in Backbone and an introduction to the View module. (Full code is here)
Backbone has a utility/helper library called underscore.js and you can use their template solution out of box. You can also use any other template solution that you want like mustache or handlebars. Let’s stick with _.js for simplicity’s sake.
_.js templates have the following syntax,
_.template(templateString, [data], [settings])
where in the
templateString you use the place holder
<%= %> and
<%- %> to dynamically insert data. The later allows for HTML escape while the first one doesn’t. Moreover, you can use
Let’s see it in action and rewrite our “Hello World” using a _.js template instead.
Run the app again and verify that it’s working with the template.
Continue with the 2nd part and learn more about Backbone’s Models, Collections, View and Events!