Common Backbone.js usage patterns.
Switch branches/tags
Nothing to show
Pull request Compare This branch is 29 commits behind rstacruz:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Backbone patterns

Here, I try to document the good practices that our team has learned along the way building Backbone applications.


This document assumes that you already have some knowledge of Backbone.js, jQuery, and of course, JavaScript itself.

Model patterns

Bootstrapping data

The problem: Your application needs models to be available on page load.

Solution: Bootstrap collections and models by creating collections in an inline <script> block.

This is mentioned in the official Backbone documentation under the Loading bootstrapped models section.

Define collection data

Define your collection data in an inline script in the HTML file. If you have collections for Photos, you may be doing it this way:


    <script> = new Photos([
        { id: 2, name: "My dog", filename: "IMG_0392.jpg" },
        { id: 3, name: "Our house", filename: "IMG_0393.jpg" },
        { id: 4, name: "My favorite food", filename: "IMG_0394.jpg" },
        { id: 5, name: "His bag", filename: "IMG_0394.jpg" },

Accessing instances

To get a single Photo, instead of creating a Photo instance and using fetch(), simply pluck it from the giant collection.

// Gets by ID
var photo =;

// Gets a bunch of photos based on criteria
var photo = {
  return photo.filename.match(/^IMG/);

In Ruby (ERB)

In your server-side templates, you will probably be using to_json on a collection of your server-side models.

<script> = new Photos(<%= @photos.to_json %>);

In Ruby (HAML)

If you use HAML, you will need use a syntax similar to this.

:javascript = new Photos(#{@photos.to_json});


In your server-side templates, you will probably be using json_encode() on a collection of your server-side models.

<script> = new Photos(<?php echo json_encode($photos); ?>);

View patterns

Inline templates

The problem: if you need to use view templates in a small Backbone application, defining your templates in JavaScript code will be unwieldy and difficult to maintain.

Solution: You may need some view templates to be inline in the HTML page.

This solution has been outlined by John Resig in his blog post about JavaScript micro templating.

Defining inline templates

You can put templates in an HTML <script> tag.

  • Change the type attribute to something else so it will not be interpreted as JavaScript.

  • Set an id so we can easily refer to it.

<script type="text/html" id="template-contact">
  <div class='contact'>
    <strong><%= name %></strong>
    <span><%= email %></span>

Using inline templates

In JavaScript, you can get the innerHTML (or jQuery's .html()) of that HTML element to fetch the raw template data. You can pass this onto Underscore's _.template to create a template function.

//=> "<div class='contact'>\n<strong><%= name %></str..."

template = _.template($("#template-contact").html());
//=> function() { ... }

Integrating into Backbone

In practice, you will most likely be using this in the render() method of a view like so.

var ContactView = Backbone.View.extend({
  template: _.template($("#template-contact").html()),

  render: function() {
    // This is a dictionary object of the attributes of the models.
    // => { name: "Jason", email: "" }
    var dict = this.model.toJSON();

    // Pass this object onto the template function.
    // This returns an HTML string.
    var html = this.template(dict);

    // Append the result to the view's element.

    // ...


Single-page apps only. This assumes that your Backbone application is all contained in one HTML page. If your app spans across multiple HTML pages, and each page will be needing the same templates, you may be redundantly streaming the template data to the browser unnecessarily. Consider using JST templates instead.

Note that the given example assumes that the #template-contact element appears before you include JavaScript files, as it requires the template element to be accessible before the class is defined.

JST templates

The problem: if you need to use view templates in a small-to-large Backbone application, defining your templates in JavaScript code will be unwieldy and difficult to maintain.

Solution: You may need put the templates in a JavaScript file.

The structure

Your app will need to serve a dynamically-created JavaScript file that compiles your files.

A common JST file will create the JST object (in the window namespace), with each of its members defined as template functions. In this example, we'll use Underscore's _.template, which returns functions.

window.JST = {};

window.JST['person/contact'] = _.template(
    "<div class='contact'><%= name %> ..."

window.JST['person/edit'] = _.template(
    "<form method='post'><input type..."

You will then need to link to this JavaScript page in your HTML.

<script src=""></script>

Using JST templates

In your JavaScript code, simply access the JST object's members to access the views.

var html = JST['person/edit']();

var dict = { name: "Jason", email: "" };
var html = JST['person/contact'](dict);

Integration notes

  • Rails 3.1 and above: The Rails Asset pipeline already comes with support for JST pages.
  • Rails 3.0 and below: consider using Sprockets or Jammit.
  • In Sinatra: The sinatra-backbone gem can take care of dynamically serving JST templates.


The problem: there may be parts of HTML templates that can be reused in many parts of the application. Defining them more than once is not DRY, which may make your application less maintainable.

Solution: separating these snippets into partials.

Partials are templates that are meant to be used inside other templates.

One typical use of partials is for lists where the template for list items may be defined as a separate template from the list itself.


You can pass the template function for the partial as a parameter to the first template.

In this example, the function itemTemplate is passed onto the parameters for template().

TasksList = Backbone.View.extend({
  template: _.template([
    "<ul class='task_list'>",
      "<% items.each(function(item) { %>",
        "<%= itemTemplate(item) %>",
      "<% }); %>",

  itemTemplate: _.template(
    "<li><%= name %></li>"

  render: function() {
    var html = this.template({
      items: tasks /* a collection */,
      itemTemplate: this.itemTemplate


Animation buffer

The problem: When you have events that trigger animations, they can mess up when the user clicks too fast.

The solution: Make a buffering system to ensure that animations are fired serially (one after the other) and never parallel (at the same time).

The situation

Let's say you have this innocent code that performs an animation.

One fundamental flaw here is that it assumes that .next() will only be called when it is not animating. When the user clicks "Next" while the animation is working, unexpected results will occur.

PicturesView = Backbone.View.extend({
  events: {
    'click .next':     'next'

  next: function() {
    var current = this.$(".current");
    var next    = this.$(".current + div");

    if (next.length == 0) { return; }

    // Make the current one move to the left via jQuery.
    // This uses jQuery.fn.animate() that changes CSS values, then fires
    // the function supplied when it's done.
    current.animate({ left: -300, opacity: 0 }, function() {

The solution

Here's a simple buffering solution. It provides two commands:

  • add(fn) which adds a given function to the buffer, and
  • next() which moves onto the next command.

To use this, put your animations inside an anonymous function to be passed onto add(). Be sure to trigger next() when the animations are done.

Buffer = {
  commands: [],

  // Adds a command to the buffer, and executes it if it's the only command
  // to be ran.
  add: function(fn) {
    if (this.commands.length == 1) fn();

  // Moves onto the next command in the buffer.
  next: function() {
    if (this.commands.length) this.commands[0]();


This is our example from a while ago that has been modified to use the bufferer.

next: function() {
  var current = this.$(".current");
  var next    = this.$(".current + div");

  if (next.length == 0) { return; }

  // Ensure that the animation will not happen while another animation is
  // ongoing.
  Buffer.add(function() {
    current.animate({ left: -300, opacity: 0 }, function() {

      // Trigger the next animation.;

Sub views

The problem: Your view code is starting to bloat as it tries to do too many things in one class.

The solution: Break it apart into smaller sub-views.

The situation

This is a common occurrence if you have one giant view that takes care of the entire page. View classes may become unwieldy once they get up to 200 lines.

Solution 1: Sub views

It may be wise to delegate some areas of the view to be the responsibility of another view.

In this example, we have a view that handles the entire application "chrome." Let's break apart some of its parts on its render() function. Notice that we're using this.$() to select elements inside the ChromeView's element itself.

App.ChromeView = Backbone.View.extend({
  render: function() {
    // Instantiate some "sub" views to handle the responsibilities of
    // their respective elements.
    this.sidebar = new App.SidebarView({ el: this.$(".sidebar") }); = new App.NavigationView({ el: this.$("nav") });

$(function() { = new App.ChromeView({ el: $("#chrome") });

We will then be able to access the sub-views like so:;;


All Backbone objects can emit events. To maintain the separation of responsibilities of the view classes, you may have the sub-views trigger events that the parent view would need (and vice versa).

For instance, we may implement SidebarView to trigger events when the sidebar is collapsed or expanded:

App.SidebarView = Backbone.View.extend({
  toggle: function() {
    if ($(this.el).is(':visible')) {
      this.trigger('collapse');    // <==
    } else {
      this.trigger('expand');      // <==

And the parent view (ChromeView) may listen to them like so:

App.ChromeView = Backbone.View.extend({
  render: function() {
    this.sidebar = new App.SidebarView({ el: this.$(".sidebar") });

      .bind('collapse', this.onSidebarCollapse)
      .bind('expand',   this.onSidebarExpand);

    // ...

Splitting views

The problem: Your view code is starting to bloat as it tries to do too many things in one class, and making sub-views with its child elements is not an option.

The solution: Make a sub-view with the same element.


You can make 2 or more views that target the same element. This is useful when there are many controls in a view, but creating sub-views (with their scopes limited to a set of elements in the bigger view) may be too messy, or just not possible.

In this example, ChromeView will make a sub-view that shares the same element as it does.

App.ChromeView = Backbone.View.extend({
  events: {
    'click button': 'onButtonClick'
  render: function() {
    // Pass our own element to the other view.
    this.tabs = new App.TabView({ el: this.el });

App.TabView = Backbone.View.extend({
  // Notice this view has its own events. They will not
  // interfere with ChromeView's events.
  events: {
    'click nav.tabs a': 'switchTab'


Naming convention

Classes often start in uppercase letters, while instances start with lowercase letters. This is a throwback of the general Python and Ruby practice of having constant names start with uppercase letters.

// Classes:

// Instances:

For names with multiple words, JavaScript often calls for CamelCase. Using underscores are discouraged in JavaScript.

// Good (CamelCase):

// Avoid (under_scores):

Namespace convention

The convention we use puts everything in one App namespace to keep things organized properly.

window.App = {

Subsequent models, views, and other classes will be made in this namespace.

App.Photo = Backbone.Model.extend({

Some people prefer to use namespaces based on their app's name. Consider, say, BF.Photo (instead of App.Photo) if your application name is "Bacefook."

Models:                    App.Photo
Collections:               App.Photos
Views:                     App.PhotoView
Main router:               App.Router
Custom routers:            App.SpecialRouter

Router instance:           App.router
View instances:            App.photoView
Singleton model instances:
Collection instances:

Variation: two-level namespace

Some people prefer a verbose two-level version where the classes are divided into their own namespaces as well.

This is often done to make it easy to iterate over all available models, collections, and views.

Models:                    App.Models.Photo
Collections:               App.Collections.Photos
Views:                     App.Views.Photo

RequireJS and AMD

You may adopt a Asynchronous Module Definition-style method of organization using a library like RequireJS. This will allow you to organize your modules in the require(...) way familiar to those who use NodeJS.

If you adopt an AMD library, there will be no need to use namespaces for your JavaScript classes.

define(function() {
  var Photo = require('models/photo');
  var Photos = require('collections/photos');
  var MenuView = require('views/menu');
  var MainRouter = require('router/main');

  // ...

For more information on RequireJS, AMD, and using it on your Backbone project, see:

File naming

For applications that do not use Asynchronous Module Definition-style organization, there always seem to have 3 basic JavaScript files.

The main namespace

This is often app.js, which defines the basic namespace.

// app.js
window.App = {

The individual classes

If you use the namespacing method outlined earlier in this document, there are 2 common naming conventions for individual classes:

  • Name the files as the exact class name they contain. For instance, App.PhotoView should be stored as app/photoview.js.

  • Place each of the class types in their own folders. For instance, the PhotoView may be defined as app/views/photoview.js, or views/photoview.js.

In this approach, avoid putting code in the files other than the actual class it defines. This makes your convention predictable for the benefit of those new to your project.

// app/photoview.js
App.PhotoView = Backbone.View.extend({

The setup/glue code file

This is the file where you do miscellaneous things that do not belong in any of the Backbone classes:

  • Instantiate the default view
  • Initialize the Backbone Router
  • Provide options for jQuery and its plugins

This is often named application.js or setup.js.

In larger projects, this can span multiple files. Don't be afraid to refactor it to multiple files.

This is often the only place you will want to put the onload hook $(function() { ... }).

$(function() {
  // Set up some options for jQuery and plugins.
  $(document).ajaxError(function() {
    alert("There was an error.");

  // Provide options for your plugins.
  $("a[rel~=lightbox]").click(function() {

  Backbone.emulateJSON = true;

  // Initialize Backbone views.
  App.chromeView = new App.ChromeView({ el: $("body") });
  App.router = new App.Router;

  // Initialize the Backbone router.

Load order

Consider loading them in this order:

  • app.js (the namespace)
  • app/*.js (individual classes)
  • setup.js (the glue)
<script src="javascripts/app.js"></script>
<script src="javascripts/app/photo.js"></script>
<script src="javascripts/app/photoview.js"></script>
<script src="javascripts/app/photos.js"></script>
<script src="javascripts/setup.js"></script>


Here are a few common practices that I believe should generally be avoided.

$() abuse

jQuery allows you to defer execution of code until when the DOM is fully-loaded with $(document).ready(...), or its short form, $(...). This is useful for getting everything set up once your HTML document is ready.

$(document).ready(function() {
  // Initialize the router.
  App.router = new App.MainRouter;

  // Initialize the main view.
  App.dashboard = new App.Dashboard({ ... });

  // and so on...

// Or its shorter form:
$(function() {
  // ...

A common anti-pattern is to put class definitions (for views, models, and such) inside these blocks. They are not necessary.

// AVOID this:
$(function() {
  App.PhotoView = Backbone.View.extend({

Your classes should be ready before the HTML DOM is. This will save you from running into problems later where certain classes may not be available at certain parts of your application.

// Consider instead:
App.PhotoView = Backbone.View.extend({

Things outside views

Put things in your view class code as much as possible.

Event handlers outside views

Every time you make an event handler outside a view class, consider making a new view class.

App.PhotoView = Backbone.View.extend({

// AVOID this!
$("").click(function() { ... });

Other links

Other links of interest:


© 2011-2012, Rico Sta. Cruz. Released under the MIT License.

This document is authored and maintained by Rico Sta. Cruz with help from its contributors. It is sponsored by my startup, Sinefunc, Inc.

To do list

  • Model associations
  • Adding events to subclasses
  • View modes
  • Nested views
  • Router entry/exit
  • View helpers