Chapter 5. Transforming Collections
Now that the details of a movie can be rendered, let’s look at a more realistic movies program. When there are tens or hundreds of movies, users might want to quickly sort and filter them, as well as paginate through a large collection. The process of filtering, sorting, and paginating revolves around adding and removing models from a collection, so we are going to study how to transform the structure of a Backbone collection next.
In the Munich Cinema example, our main goal is to give users a way to quickly find an interesting movie. We especially want to provide basic search and filtering options for better navigation through the movie program.
The goal of this chapter is to provide an overview on the following topics:
- Sorting a collection
- Filtering a collection
- Using Backbone.Obscura to wrap sorting, filtering, and paginating
Functional Enhancements
When you read the documentation of Backbone.Collection, you will stumble upon an important piece of information to access and mutate a collection:
Backbone proxies to Underscore.js to provide 28 iteration functions on Backbone. Collection.
You already saw some examples of using map
. In the sections to follow, you will learn the relevance of sortBy
and filter
.
Sorting
First, we look at sorting models (in this case, movies). Sorting models is a common task for a Backbone collection. Usually, you need to define a comparator function to get the correct positions of models in a collection.
From the documentation ...
Get Full Stack Web Development with Backbone.js now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.