O'Reilly logo

Backbone.js Patterns and Best Practices by Swarnendu De

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

Filtering a collection

Filtering a collection is a fairly simple concept; here we want to get a part of the data based on a certain criteria. For example, if you have a list of items and you want to filter the list to only show a subset of all the items, you filter the attached collection. By default, Backbone provides some built-in functions that take care of basic filtering. The two methods where() and findWhere() produce similar functionality, though findWhere() only returns the first model that matches the condition.

Performing basic filtering

The where() method accepts a set of model attributes and returns an array of the matched models.

var users = new Backbone.Collection([ { name: 'John', company: 'A' }, { name: 'Bill', company: 'B' }, { ...

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