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

Book Description

For JavaScript developers the Backbone.js framework is full of potential. This book, with its accessible recipes, allows you to get to grips with building modern frontend web applications, quickly and easily.

  • Easy-to-follow recipes to build dynamic web applications

  • Learn how to integrate with various frontend and mobile frameworks

  • Synchronize data with a RESTful backend and HTML5 local storage

  • Learn how to optimize and test Backbone applications

  • In Detail

    There is no doubt that the superior rendering power of HTML5, thin-to-thick client transition and REST style communication created a new era in web development, replacing the outdated approach based on browser plugin technologies. Backone.js allows developers to write lightweight, modular, and scalable JavaScript applications.

    Backbone.js Cookbook contains a series of recipes that provide practical, step-by-step solutions to the problems that may occur during frontend application development using an MVC pattern. You will learn how to build Backbone applications utilizing the power of popular Backbone extensions and integrating your app with different third party libraries. You will also learn how to fulfill the requirements of the most challenging tasks.

    The first chapter of the book introduces you to the MVC paradigm and teaches you how to architect rich Internet applications operating with basic concepts of Backbone.js. During the reading of this book you will learn how to solve challenging problems leveraging Backbone objects such as models, collections, views, routers, and so on.

    You learn how to use forms, layouts, templating engines, and other Backbone extensions, which will help you to complete specific features of your application. You will understand how to bind a model to a DOM element. You will see how perfectly Backbone.js integrates with third party libraries and frameworks such as jQuery, Zepto, Underscore.js, Require.js, Mustache.js, Twitter Bootstrap, jQueryMobile, PhoneGap and many others. This book will guide you in how to optimize and test your applications, create your own Backbone extensions, and share them with the open source community.

    With the help of Backbone.js Cookbook, you will learn everything you need to know to create outstanding rich Internet applications using the JavaScript programming language.

    Table of Contents

    1. Backbone.js Cookbook
      1. Table of Contents
      2. Backbone.js Cookbook
      3. Credits
      4. About the Author
      5. Acknowledgement
      6. About the Reviewers
      7. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      8. 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
      9. 1. Understanding Backbone
        1. Introduction
        2. Designing an application with the MVC pattern
          1. How to do it...
        3. Defining business logic with models and collections
          1. How to do it...
          2. How it works...
          3. See also
        4. Modeling an application's behavior with views and a router
          1. How to do it...
          2. How it works...
          3. See also
        5. Creating an application structure from scratch
          1. How to do it...
        6. Writing your first Backbone application
          1. How to do it...
          2. See also
        7. Implementing URL routing in your application
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Parsing parameters in a URL
          4. See also
        8. Extending an application with plugins
          1. How to do it...
          2. See also
        9. Contributing to the Backbone project
          1. How to do it...
      10. 2. Models
        1. Introduction
        2. Creating a model
          1. How to do it...
          2. There's more...
            1. Cloning a model
            2. Setting default attribute values
            3. Setting default attribute values with a multiline expression
          3. See also
        3. Operating with model attributes
          1. How to do it...
          2. How it works...
          3. There's more…
            1. Checking if a model has an attribute
            2. Getting HTML escaped attribute value
        4. Operating with the model identifier
          1. How to do it...
          2. How it works...
          3. There's more...
        5. Validating model attributes
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Handling validation errors
            2. Triggering validation manually
          4. See also
        6. Overriding getters and setters
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Overriding getter and setter of an existing attribute
            2. Handling mutators events
          5. See also
        7. Creating undo points to store/restore a model's state
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Working with the Memento stack
            2. Restoring from the first state in the stack
            3. Ignoring attributes from being restored
            4. Working with collections
          5. See also
        8. Implementing workflow for a model
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Binding callbacks to transition events
          5. See also
        9. Using advanced validation in a model
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using built-in validators
          5. See also
        10. Validating an HTML form
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        11. Working with nested attributes in a model
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Working with a nested array of attributes
            2. Adding/removing elements to/from a nested array
            3. Binding callbacks to an events
          5. See also
        12. Implementing a one-to-one relationship
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      11. 3. Collections
        1. Introduction
        2. Creating a collection of models
          1. How to do it...
          2. How it works...
          3. There's more...
        3. Getting a model from a collection by its index
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Getting an index of a collection model
            2. Getting an independent copy of a model
            3. Getting the length of a collection
        4. Getting a model from a collection by its ID
          1. How to do it...
          2. How it works...
          3. See also
        5. Adding a model to a collection
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Adding a model at a specific position
            2. Adding multiple models
            3. Adding existing models
          4. See also
        6. Removing a model from a collection
          1. How to do it...
          2. How it works...
          3. There's more...
        7. Working with a collection as a stack or as a queue
          1. How to do it...
          2. How it works...
        8. Sorting a collection
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Comparing a pair of models in the comparator
          4. See also
        9. Filtering models in a collection
          1. How to do it...
          2. See also
        10. Iterating through a collection
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Checking every model to match a specific condition
            2. Checking any model to match a specific condition
            3. Getting the attribute from each model in a collection
            4. Performing specific calculations to each model in a collection
            5. Boiling down models in a collection into a single value
          4. See also
        11. Chaining a collection
          1. How to do it…
          2. How it works...
          3. See also
        12. Running No SQL queries on a collection
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using standard operators
              1. $equal
              2. $ne
              3. $in
              4. $nin
              5. $exists or $has
            2. Combining queries
              1. $and
              2. $or
              3. $nor
              4. $not
            3. Multiple queries on the same key
            4. Sorting query results
            5. Paging query results
            6. Caching results
          5. See also
        13. Storing models of various types in the same collection
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Mapping deeply nested attributes
            2. Use a function to map the models
        14. Implementing a one-to-many relationship
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Implementing a many-to-many relationship
            2. Exporting related models to JSON
          5. See also
      12. 4. Views
        1. Introduction
        2. Rendering a view
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Creating a new HTML element associated with a view
            2. Changing the view element dynamically
            3. Removing a view
          4. See also
        3. Dealing with a view element using jQuery
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Using Zepto as a faster alternative to jQuery
          4. See also
        4. Rendering a model in a view
          1. How to do it...
          2. How it works...
          3. See also
        5. Rendering a collection in a view
          1. How to do it...
          2. How it works...
          3. See also
        6. Splitting a view into subviews
          1. How to do it...
          2. How it works...
        7. Handling Document Object Model (DOM) events in a view
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Delegating and undelegating events manually
          4. See also...
        8. Switching views using Backbone.Router
          1. How to do it...
          2. How it works...
          3. See also
      13. 5. Events and Bindings
        1. Introduction
        2. Managing events in Backbone.js
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Unbinding callback from the event
            2. Listening to events on other objects
        3. Handling events of Backbone objects
          1. How to do it...
          2. There's more...
            1. Avoiding event triggering when working with Backbone objects
            2. Using built-in events
          3. See also
        4. Binding a model to a view
          1. How to do it...
          2. How it works...
        5. Binding a collection to a view
          1. How to do it...
          2. How it works...
        6. Bidirectional binding with Backbone.stickit
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Overriding model getters and setters
            2. Overriding view element updates
            3. Listening to a specific HTML event
          5. See also
        7. Binding a model and a collection to a select list
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Handling keyboard shortcuts in a view
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        9. Handling router events
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
      14. 6. Templates and UX sugar
        1. Introduction
        2. Using templates in a view
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Splitting a template into partials
          4. See also
        3. Implementing a template loader
          1. How to do it...
          2. How it works...
        4. Using Mustache templates
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using compiled templates
            2. Using partials
          5. See also
        5. Defining a form
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using a form without a model
          5. See also
        6. Adding validation to a form
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Customizing error messages
            2. Performing a model validation
          4. See also
        7. Handling form events
          1. How to do it...
          2. How it works...
          3. See also
        8. Customizing a form with the Bootstrap framework
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Overriding form templates
          5. See also
        9. Assembling layouts with LayoutManager
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        10. Building a semantic and an easily styleable data grid
          1. Getting ready
          2. How to do it...
        11. How it works...
          1. There's more...
            1. Performing bulk operations on grid models
            2. Performing records filtering
          2. See also
        12. Drawing on the HTML5 canvas
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
      15. 7. REST and Storage
        1. Introduction
        2. Architecting the REST API for the backend
          1. How to do it...
          2. How it works...
        3. Prototyping a RESTful backend with MongoLab
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. See also
        4. Synchronizing models and collections with a RESTful service
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Handling MongoDB Extended JSON
          4. See also
        5. Building a RESTful frontend with Backbone
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. See also
        6. Using the polling technique to fetch data
          1. Getting ready...
          2. How to do it...
          3. How it works...
        7. Working with local storage
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. See also
      16. 8. Special Techniques
        1. Introduction
        2. Using mixins with Backbone objects
          1. How to do it...
          2. How it works...
          3. See also
        3. Creating a Backbone.js extension with Grunt
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. See also
        4. Writing tests for a Backbone extension with QUnit
          1. How to do it...
          2. How it works...
          3. See also
        5. Mocking up a RESTful service with jQuery Mockjax in asynchronous tests
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. See also
        6. Developing a mobile application with jQuery Mobile
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. See also
        7. Building an iOS/Android app with PhoneGap
          1. Getting ready...
          2. How to do it...
          3. See also
        8. Organizing a project structure with Require.js
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Optimizing JS files with r.js
          5. See also
        9. Ensuring compatibility with search engines
          1. Getting ready...
          2. How to do it...
          3. How it works...
          4. See also
        10. Avoiding memory leaks in a Backbone application
          1. Getting ready...
          2. How to do it…
          3. See also
      17. Index