O'Reilly logo

Full Stack Web Development with Backbone.js by Patrick Mulder

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 5. Transforming Collections

Now that 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 movies, as well as paginate through a large collection of movies. Since filtering, sorting and pagination is about adding and removing models from a collection, we are going to study how to transform the structure of a Backbone collection next.

In our example of Munich Cinema, our main goal is to give users a way to quickly find an interesting movie. Especially, we 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:

  • Sorting a collection
  • Filtering a collection
  • Using Backbone.Obscura to wrap sorting, filtering and pagination

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 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 at Backbone.js, the working of a comparator is described ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required