You are previewing Mastering Backbone.js.
O'Reilly logo
Mastering Backbone.js

Book Description

Design and build scalable web applications using Backbone.js

About This Book

  • Level up your Backbone.js skills and create professional web applications with the best practices

  • Use the Backbone.js components in the right way and avoid maintenance nightmares

  • Improve your development workflow from application design to deployment

  • Apply the best practices given in this tutorial to solve day-to-day problems in your applications

  • Who This Book Is For

    This book is for those developers who know the basic concepts of Backbone.js and want to build scalable applications with it. If you are looking for the best practices of using Backbone.js applied to real work applications, this book is for you. You will be able to apply architectural principles to create amazing web applications easily.

    What You Will Learn

  • Build web applications that scale with Backbone.js

  • Design a powerful architecture that eliminates maintenance nightmares

  • Use common patterns and best practices in Backbone.js web applications developments

  • Pack your applications to be deployed to production environments

  • Clean up your code organization to a simple and maintainable architecture

  • Test your components and get confidence with your code

  • Deal with common scenarios like file uploading and login issues

  • In Detail

    Backbone.js is a popular library to build single page applications used by many start-ups around the world because of its flexibility, robustness and simplicity. It allows you to bring your own tools and libraries to make amazing webapps with your own rules. However, due to its flexibility it is not always easy to create scalable applications with it. By learning the best practices and project organization you will be able to create maintainable and scalable web applications with Backbone.js.

    With this book you will start right from organizing your Backbone.js application to learn where to put each module and how to wire them. From organizing your code in a logical and physical way, you will go on to delimit view responsibilities and work with complex layouts.

    Synchronizing models in a two-way binding can be difficult and with sub resources attached it can be even worse. The next chapter will explain strategies for how to deal with these models. The following chapters will help you to manage module dependencies on your projects, explore strategies to upload files to a RESTful API and store information directly in the browser for using it with Backbone.js. After testing your application, you are ready to deploy it to your production environment. The final chapter will cover different flavors of authorization.

    The Backbone.js library can be difficult to master, but in this book you will get the necessary skill set to create applications with it, and you will be able to use any other library you want in your stack.

    Style and approach

    This book takes a tutorial approach to help you scale your Backbone.js applications. It builds a web application using the best practices and applies architectural design principles to develop maintainable web-apps. Each chapter explains the design decisions and improves the project that is used as an example alongside the book.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Mastering Backbone.js
      1. Table of Contents
      2. Mastering Backbone.js
      3. Credits
      4. About the Author
      5. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Architecture of a Backbone application
        1. Subapplications based architecture
          1. Subapplication anatomy
        2. Responsibilities of Backbone objects
          2. Models
          3. Collections
          4. Routers
        3. Objects not provided by Backbone
          1. Subapplication façade
          2. Subapplication controller
        4. Contacts application
          1. File organization
        5. Summary
      9. 2. Managing Views
        1. Identifying view types
        2. ModelView
        3. CollectionView
          1. Adding new models
          2. Deleting models
          3. Destroying views
          4. Resetting the collection
        4. Region
        5. Layout
        6. Putting it all together
          1. Showing a list
          2. Showing the details
          3. Editing information
        7. Rendering third-party plugins
        8. Conclusions
      10. 3. Model Bindings
        1. Manual binding
        2. Two-way binding
          1. References
        3. Data binding with plugins
        4. Binding embedded data
          1. Binding an embedded list
        5. Validating model data
          1. Manual validation
          2. Validating with the Backbone.Validation plugin
        6. Summary
      11. 4. Modular Code
        1. CommonJS modules
        2. NPM and package.json
        3. Browserify
        4. Application dependency
        5. Using Browserify in the app
          1. Solving cyclic dependency
        6. Modularizing templates
        7. Summary
      12. 5. Dealing with Files
        1. Express server
        2. Attaching a file into a resource
        3. Uploading the avatar photo to contacts
          1. Showing the avatar
        4. Uploading images from Backbone
          1. Uploading a file with AJAX
        5. Uploading the avatar image at creation time
        6. Encoding the upload file
        7. Summary
      13. 6. Store data in the Browser
        1. The localStorage
          1. Starting with localStorage
        2. Backbone and localStorage
        3. Store models in localStorage
        4. Store Backbone models in localStorage
        5. Backbone.sync
          1. Using localStorage as cache
        6. IndexedDB
          1. Getting started with IndexedDB
          2. Database versions
          3. Creating stores
          4. Delete a database
          5. Add elements to an object store
          6. Performing queries
          7. Delete objects in the store
        7. IndexedDB in Backbone
        8. Summary
      14. 7. Build Like a Pro
        1. Development workflow
        2. What's a task runner?
        3. How Gulp works
        4. Getting started with Gulp
        5. Creating a development workflow
          1. Bundling the JavaScript files with Browserify
            1. Sourcemaps
            2. Re-bundle automatically
          2. BrowserSync
          3. Run server with Express
        6. Creating a production workflow
          1. Gulp useref
          2. Image Optimization
          3. Fonts
          4. Bundle JavaScript files for production
          5. Putting it all together
        7. Summary
      15. 8. Testing Backbone Applications
        1. Testing tools
        2. Getting started with Jasmine
          1. Expectations
          2. Testing asynchronous code
        3. Karma test runner
        4. What and how to test Backbone applications
        5. Testing models and collections
        6. Testing views
        7. Testing controllers
          1. Mocking dependencies
          2. Fake objects
          3. Testing ContactEditor
        8. Testing the subapplication Façade
        9. Summary
      16. 9. Deploying to Production
        1. Heroku
          1. Dynos
          2. Getting started with Heroku
        2. Production environment
        3. The HTTP Server
        4. Do not run as root
        5. Process Management
        6. Summary
      17. 10. Authentication
        1. Stateless API authentication
          1. HTTP Basic authentication
          2. The OAuth2 authentication
            1. Service applications
          3. OAuth2 grant types
            1. Authorization code grant
            2. Implicit Grant
            3. Resource Owner Password Credentials Grant
            4. Client credentials grant
            5. Resume
        2. Implementing HTTP Basic Authentication
        3. Implementing OAuth authentication
        4. Summary
      18. Index