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

Working with Collections


In Backbone, Models may form the core of all data interaction, but you'll soon find that you need to work with multiple Models to do any real work, which is where the Collections class comes in. Just as a Model wraps an object and provides additional functionality, Collections wraps an array and offers several advantages over using the array directly:

  • Collections uses Backbone's class system, making it easy to define methods, create subclasses, and so on

  • Collections allows other code to listen for and respond when Models are added or removed from that Collection or when Models in a Collection are modified

  • Collections simplifies and encapsulates the logic for communicating with the server

We can create a new Collection subclass, as follows:

var Cats = Backbone.Collection.extend({
     // The properties and methods of Cats would go here
});

Once we've created a subclass, we can instantiate new instances of it using JavaScript's new keyword, just as we did to create new...