Book Image

Backbone.js Patterns and Best Practices

By : Swarnendu De
Book Image

Backbone.js Patterns and Best Practices

By: Swarnendu De

Overview of this book

Table of Contents (19 chapters)
Backbone.js Patterns and Best Practices
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Precompiling Templates on the Server Side
Index

Chapter 4. Working with Collections

The purpose of the Backbone collection is pretty straightforward. As an ordered set of models, a collection provides a number of useful methods to play around with, including a set of Underscore.js utility methods. A collection includes functionality to add, remove, sort, and filter models, and save to or fetch data from the server. A collection listens to the events fired on its models–if an event is fired on a model of a collection, it will also be fired on the collection itself. This facility is quite significant when you want to listen to an attribute-change event of the model. We will look into it using some examples in the Basic usage of collections section of this chapter.

In the previous chapters, we saw a number of implementations of a simple collection to display multiple items in a list view. However, there can be cases where you want to sort the list with a number of criteria or you want to filter the list items to show only a particular type...