Welcome to my (in-progress) book about the Backbone.js framework for structuring JavaScript applications. Its released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported license meaning you can both grab a copy of the book for free or help to further improve it.
Im very pleased to announce that this book will be out in physical form in a few months time via OReilly Media. Readers will have the option of purchasing the latest version in either print or a number of digital formats then or can grab a recent version from this repository.
Corrections to existing material are always welcome and I hope that together we can provide the community with an up-to-date resource that is of help. My extended thanks go out to Jeremy Ashkenas for creating Backbone.js and these members of the community for their assistance tweaking this project.
Chapter 1. Introduction
As JavaScript developers, we are at an interesting point in time where not only do we have mature solutions to help organize the JavaScript powering our applications based on a separation of concerns, but developers looking to build non-trivial projects are almost spoiled for choice for frameworks that can help structure their applications.
Maturity in software (framework) development isnt simply about how long a framework has been around. Its about how solid the framework is and more importantly how well its evolved to fill its role. Has it become more effective at solving common problems? Does it continue to improve as developers build larger and more complex applications with it?
In this book, I will be covering the popular Backbone.js, which I consider the best of the current family of JavaScript architectural frameworks.
Topics will include MVC theory and how to build applications using Backbones models, views, collections and routers. Ill also be taking you through advanced topics like modular development with Backbone.js and AMD (via RequireJS), how to build applications using modern software stacks (like Node and Express), how to solve the routing problems with Backbone and jQuery Mobile, tips about scaffolding tools, and a lot more.
If this is your first time looking at Backbone.js and youre still unsure whether or not to give it a try, why not take a look at how a Todo application can be implemented in Backbone and several other popular Javascript frameworks before reading further?
The goal of this book is to create an authoritative and centralized repository of information that can help those developing real-world apps with Backbone. If you come across a section or topic which you think could be improved or expanded on, please feel free to submit a pull-request. It wont take long and youll be helping other developers avoid problems youve run into before.
Fundamentals
In this section we are going to cover the context into which a framework like Backbone.js fits. Lets begin our journey into understanding Backbone better with a look at code architecture.
MVC, MVP & Backbone.js
Before exploring any JavaScript frameworks that assist in structuring applications, it can be useful to gain a basic understanding of architectural design patterns. Design patterns are proven solutions to common development problems and can suggest structural approaches to help guide developers in adding some organization to their applications.
Patterns are useful because theyre a set of practices that build upon the collective experience of skilled developers who have repeatedly solved similar problems. Although developers 10 or 20 years ago may not have been using the same programming languages when implementing patterns in their projects, there are many lessons we can learn from their efforts.
In this section, were going to review two popular patterns - MVC and MVP. Well be exploring in greater detail how Backbone.js implements these patterns shortly to better appreciate where it fits in.
MVC
MVC (Model-View-Controller) is an architectural design pattern that encourages improved application organization through a separation of concerns. It enforces the isolation of business data (Models) from user interfaces (Views), with a third component (Controllers) traditionally present to manage logic, user-input and the coordination of models and views. The pattern was originally designed by Trygve Reenskaug while working on Smalltalk-80 (1979), where it was initially called Model-View-Controller-Editor. MVC was described in depth in Design Patterns: Elements of Reusable Object-Oriented Software (The GoF or Gang of Four book) in 1994, which played a role in popularizing its use.
Smalltalk-80 MVC
Its important to understand what the original MVC pattern was aiming to solve as it has changed quite heavily since the days of its origin. Back in the 70s, graphical user-interfaces were far and few between. An approach known as Separated Presentation began to be used as a means to make a clear division between domain objects which modeled concepts in the real world (e.g a photo, a person) and the presentation objects which were rendered to the users screen.
The Smalltalk-80 implementation of MVC took this concept further and had an objective of separating out the application logic from the user interface. The idea was that decoupling these parts of the application would also allow the reuse of models for other interfaces in the application. There are some interesting points worth noting about Smalltalk-80s MVC architecture:
A Domain element was known as a Model and were ignorant of the user-interface (Views and Controllers)
Presentation was taken care of by the View and the Controller, but there wasnt just a single view and controller. A View-Controller pair was required for each element being displayed on the screen and so there was no true separation between them
The Controllers role in this pair was handling user input (such as key-presses and click events), doing something sensible with them.
The Observer pattern was relied upon for updating the View whenever the Model changed
Developers are sometimes surprised when they learn that the Observer pattern (nowadays commonly implemented as a Publish/Subscribe system) was included as a part of MVCs architecture decades ago. In Smalltalk-80s MVC, the View and Controller both observe the Model: anytime the Model changes, the Views react. A simple example of this is an application backed by stock market data - for the application to show real-time information, any change to the data in its Models should result in the View being refreshed instantly.
Martin Fowler has done an excellent job of writing about the origins of MVC over the years and if you are interested in further historical information about Smalltalk-80s MVC, I recommend reading his work.
MVC As We Know It
Weve reviewed the 70s, but let us now return to the here and now. The MVC pattern has been applied to a diverse range of programming languages. For example, the popular Ruby on Rails is an implementation of a web application framework based on MVC for the Ruby language. JavaScript now has a number of MVC frameworks, including Ember.js, JavaScriptMVC, and of course Backbone.js. Given the importance of avoiding spaghetti code, a term which describes code that is very difficult to read or maintain due to its lack of structure, lets look at what the MVC pattern enables the Javascript developer to do.