Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Nick Plante

Dr. Dobb's Bloggers

A Backbone for JavaScript-Heavy Web UI

February 27, 2011

It seems like every week there's a new JavaScript framework that claims to simplify the process of bringing desktop-like interactivity to web applications. The number of these libraries and frameworks seems to have exploded within the last year, and although they all attack the problem in different ways (and arguably all play some small role in pushing things forward), some are definitely more interesting than others. Unfortunately only a few things that seem interesting at first glance get a closer look, because, after all, we've got actual production code to write. Right?

When a few fellow developers that I respect started talking about Backbone.js, I liked what I heard. A few weeks later, I finally had some free time to play around with it :). After spending a mere 15 minutes thumbing through a tutorial, I already liked this framework for its sheer simplicity and reliance on existing conventions. Weighing in at under 1000 lines of code, less than 4kb minified and gzip'd, Backbone does an awful lot in a very natural way, without lots of new jargon to learn or a complicated directory structure. Backbone follows basic REST conventions, so it's easy to work it into your infrastructure if you're already following this pattern on the server side (using Rails or an equivalent MVC framework).

Backbone encourages users to decouple their data from their DOM elements and use its own concept of Models. Those data models can be -- surprise -- created, validated, saved to a server, destroyed; standard CRUD operations that you're already familiar with. When a user interacts with a model, a change event is fired, which will tell all of the Views that hold a reference to that model to update themselves, thus eliminating a ton of ugly view code that we use to control visual updates in modern JavaScript-heavy web UIs. Unlike a "View" in a traditional MVC framework, an individual Backbone "View" often manages a much smaller portion of a web page, like an item in a list or an on-screen widget. Backbone also offers support for collections of models of course, and you can leverage events with these collections in similar ways, such as when an item is added or removed from a collection.

In addition to offering abstractions for Model and View components, Backbone also has a concept of a Controller, which is not entirely dissimilar from the one found in Rails or other MVC frameworks. Backbone's Controller, however, acts as a router and intermediary between #hash fragment-friendly URLs and your own actions and events. This means that is also has a notion of browser history baked-in (Backbone.history) which allows you to use the back button in your browser in a sane manner for navigation.

And of course, it all plays nicely with jQuery.

DocumentCloud, the organization that has open sourced Backbone, is already using it in production and will be continuing to improve upon it. Other services already using Backbone include QuietWrite, Tzigla, MapBox, and 37 Signals' Basecamp Mobile product. Although existing production deployments may not be a prerequisite to deploying a library in your own application, it certainly helps to know that others are depending on the long-term health of the code.

All in all I really dig the approach that Backbone takes to structured JavaScript development. It's simple, it's lightweight, and it gets out of my way and allows me to concentrate on building application logic rather than managing lots of unnecessary glue code. If you want to see a live example of Backbone in a simple application (which for me is always the best way to start evaluating a technology), their example todo list application (with local storage) is very straightforward and easily grokked. There's also an excellent introductory piece over at Liquid Media, and an even more in-depth tutorial written by the author of QuietWrite.

The Backbone.js source is hosted on GitHub. Check it out, and let me know what you think!

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.