Book Image

Backbone.js Essentials

By : Jeremy Walker
Book Image

Backbone.js Essentials

By: Jeremy Walker

Overview of this book

<p>This book offers insight into creating and maintaining dynamic Backbone.js web applications. It delves into the the fundamentals of Backbone.js and helps you achieve mastery of the Backbone library.</p> <p>Starting with Models and Collections, you'll learn how to simplify client-side data management and easily transmit data to and from your server. Next, you'll learn to use Views and Routers to facilitate DOM manipulation and URL control so that your visitors can navigate your entire site without ever leaving the first HTML page. Finally, you'll learn how to combine those building blocks with other tools to achieve high-performance, testable, and maintainable web applications.</p>
Table of Contents (20 chapters)
Backbone.js Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Indexing


In order to get or remove specific Models from a Collection, Backbone needs to know how to index these Models. Backbone does this in two ways:

  • Using the Model's id property, if any (which, as we discussed in the previous chapter, can either be set directly or indirectly by setting the Model's idAttribute property)

  • Using the Model's cid property (which all Models have)

When you add a Model or attributes to a Collection, Backbone uses both of the preceding forms of identification to register the Model in the Collection's _byId property. _byId is yet another one of Backbone's hidden properties, but it's a private property as well (because its name is prefixed by _). This means that, even more so than with other hidden properties, you should avoid using _byId directly and instead use it indirectly through methods such as get. The get method returns the Model with the provided ID (if any) by using _byId:

var garfield = new Cat({id: 'cat1', name: 'Garfield'});
var cats = new Backbone.Collection...