Another way in which several of the Underscore
methods can be used is by extracting a specific Model
or Models
from a Collection
. The simplest way to do this is with the where
and findWhere
methods, which return all the (or in the case of findWhere
, the first) Models that match a provided attributes object. For example, if you want to extract all the books in a Collection, which have exactly one hundred pages, you can use the where
method, as shown here:
var books = new Backbone.Collection([ { pages: 100, title: "Backbone Essentials 5: The Essentials Return" }, { pages: 100, title: "Backbone Essentials 6: We're Not Done Yet?" },{ pages: 25, title: "Completely Fake Title" } ]); var hundredPageBooks = books.where({pages: 100}); // hundredPageBooks array of all of the books except Completely Fake Title var firstHundredPageBook = books.findWhere({pages: 100}); firstHundredPageBook; // Backbone Essentials 5: The...