Backbone.js Patterns and Best Practices

Backbone.js Patterns and Best Practices
eBook: $23.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 15%!
Print + free eBook + free PacktLib access to the book: $63.98    Print cover: $39.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • Offers solutions to common Backbone.js related problems that most developers face
  • Shows you how to use custom widgets, plugins, and mixins to make your code reusable
  • Describes patterns and best practices for large scale JavaScript application architecture and unit testing applications with QUnit and SinonJS frameworks

Book Details

Language : English
Paperback : 174 pages [ 235mm x 191mm ]
Release Date : January 2014
ISBN : 1783283572
ISBN 13 : 9781783283576
Author(s) : Swarnendu De
Topics and Technologies : All Books, Application Development, Open Source

Table of Contents

Chapter 1: Reducing Boilerplate with Plugin Development
Chapter 2: Working with Views
Chapter 3: Working with Models
Chapter 4: Working with Collections
Chapter 5: Routing Best Practices and Subrouting
Chapter 6: Working with Events, Sync, and Storage
Chapter 7: Organizing Backbone Applications – Structure, Optimize, and Deploy
Chapter 8: Unit Test, Stub, Spy, and Mock Your App
Appendix A: Books, Tutorials, and References
Appendix B: Precompiling Templates on the Server Side
Appendix C: Organizing Templates with AMD and Require.js
  • Chapter 1: Reducing Boilerplate with Plugin Development
    • Basic components of Backbone.js
    • Using Underscore.js
    • Re-using code with extensions
      • Creating a base class
      • Developing plugins without extending base classes
    • Understanding JavaScript mixins
      • What is a mixin?
      • Creating classic mixins
      • Creating functional mixins
        • Caching mixin functions
        • Using curry to combine a function and arguments
    • Summary
  • Chapter 2: Working with Views
    • Basic usage of views
      • Understanding the el property
      • Listening to view events
      • Displaying model data with templates
    • Partially updating a view
    • Understanding nested views
      • Knowing when to use subviews
      • Avoiding multiple DOM reflow
      • Re-rendering parent views
      • Removing parent views
    • Working with templates
      • Storing templates in an HTML file
      • Storing templates in a JavaScript file
      • Precompiling templates
      • Avoiding evaluation in templates
      • Using template helper functions
    • Understanding automatic model-view data binding
    • Using Marionette's ItemView, CollectionView, and CompositeView
      • ItemView
      • CollectionView
      • Working with CompositeView
    • Using Layout Manager
    • Summary
  • Chapter 3: Working with Models
    • Basic usage of models
      • Using the defaults property
        • Avoiding object references in the defaults property
      • Data interaction with the server
    • Validating the data
      • Using the Backbone.Validation plugin
        • Configuring validation rules
        • Prevalidating a model with the preValidate() method
    • Serializing models
    • Understanding the relational data model
    • Summary
  • Chapter 4: Working with Collections
    • Basic usage of collections
      • Performing data operations with collections
        • Fetching data from the server
        • Saving data to the server
    • Sorting a collection
      • Sorting a collection with multiple attributes
  • Filtering a collection
    • Performing basic filtering
    • Filtering a collection with a duplicate collection
    • Self-filtering with full data pointers
  • Understanding the collection of multiple model types
  • Summary
  • Chapter 5: Routing Best Practices and Subrouting
    • Working with routers
    • Best practices for working with routers
      • Avoiding large functional code in route methods
      • Instantiating views in router methods
      • Using regular expressions for selective routing
    • Subrouting – a key to organizing complex apps
    • Summary
  • Chapter 6: Working with Events, Sync, and Storage
    • Understanding custom events
      • A simple case study on custom events
    • Using an event dispatcher
      • Creating multiple event dispatchers
      • Using different event namespaces
    • Avoiding memory leaks with the listenTo() method
    • Overriding the Backbone.sync() method
    • Offline storage using the Backbone.LocalStorage adapter
    • Summary
  • Chapter 7: Organizing Backbone Applications – Structure, Optimize, and Deploy
    • Understanding the application directory structure
    • Working with Asynchronous Module Definition
      • Adding Require.js to your project
      • Configuring dependencies
      • Defining a module
    • Creating application architecture
      • Managing a project directory
      • Organizing code with AMD
      • Setting up an application
        • Using the module pattern
        • Managing objects and module communication
      • Understanding view management
      • Understanding other important features
    • Summary
  • Chapter 8: Unit Test, Stub, Spy, and Mock Your App
    • Understanding why unit testing is important
    • Testing with QUnit
      • Performing a basic test case
      • Understanding the QUnit module (), setup (), and teardown () methods
      • Testing Backbone.js components with QUnit
    • Using test spies, stubs, and mocks with SinonJS
      • Testing with spies
      • Testing with stubs
      • Testing with mocks
        • Difference between mocks and stubs
    • Summary

Swarnendu De

Swarnendu De is the director of Innofied Solution Pvt. Ltd.( ), a specialized mobile, web, and game development company. He manages technical operations and leads the JavaScript development team there. For the last seven years, he has been working with numerous JavaScript technologies including Backbone.js, Node.js, ExtJS, Sencha, and so on, and has developed more than 50 complex JavaScript-based applications thus far. He regularly writes at his personal blog, company blog, and the Tuts+ network. He has been working with Backbone.js for the last 2 years and has developed multiple, large, and complex Backbone.js-based applications using this technology.

Swarnendu lives in Kolkata—the city of joy. He loves travelling, photography, and spending time with his family. You can reach him through his website at or via Twitter at @swarnendude.

Code Downloads

Download the code and support files for this book.

Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


- 4 submitted: last submission 19 May 2014

Errata type: Code | Errata page: 14 

The code is:

var paging1 = new Pagination(10, new Users());
var paging2 = new Pagination(20, new Users());

It should be:

var paging1 = new Pagination(new Users(), 10);
var paging2 = new Pagination(new Users(), 20);

Errata type: Code | Errata page: 41

<% if ( typeof(avatar) === 'undefined' ) %>

Should be:

<% if ( typeof(avatar) === 'undefined' ) { %>

Errata type: Typo | Errata page: 21

"pugins" should be "plugins"

Errata type: Typo | Errata page: 25

"it's instance" should be "its instance"

Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Backbone.js Patterns and Best Practices +    Bootstrap Site Blueprints =
50% Off
the second eBook
Price for both: €26.85

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Develop custom plugins and mixins to reduce boilerplate in your code
  • Learn about Backbone view management with nested views, subviews, layout manager, and Marionette views
  • Understand template management by storing and pre-compiling templates
  • Explore model validation with validation plugins and learn model serialization and relational data management with nested models
  • Work with collections to implement multiple sorting and filtering behavior
  • Create solid application architecture with AMD and different popular design patterns

In Detail

Backbone.js is a super light framework that allows you to structure your JavaScript codes in an MV* fashion. This framework is an excellent tool when it comes to creating an organized and modular code base for web apps of any size or complexity. Although lightweight, Backbone.js results in lots of boilerplate. Learning the best practices and design patterns will help you avoid these problems and allow you to ensure that the best standards are followed.

Backbone.js Patterns and Best Practices is packed with examples that will help you work with Backbone.js components. It also gives solutions to common problems faced by developers. It gives a complete overview of plugin development, large scale application architecture, and unit testing as well.

Starting with a discussion of why reducing boilerplate in your JavaScript code is essential, Backbone.js Patterns and Best Practices explains how to enforce reusability in your code by creating plugins and mixins. You will learn about patterns and best practices for Backbone views, models, collections, routers, and events.

You will discover solutions to common problems that developers face through simple examples, and delve into the best open source plugins available to solve these problems when they arise. This book is an excellent collection of design and implementation patterns that will help you structure and develop complex Backbone.js applications easily.


This is a step-by-step guide to design patterns, best practices, and solutions to common problems for Backbone.js-based application development.

Who this book is for

This book is for JavaScript developers who work with Backbone.js and want to learn the best design patterns to develop complex web applications. Basic knowledge of Backbone.js and JavaScript is essential.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software