Several of the remaining methods focus on testing the Collection to see whether it passes a certain type of test. The contains
and isEmpty
methods allow you to check whether the Collection
contains a specified Model
or Models
or whether it contains any models at all, respectively:
var warAndPeace = new Backbone.Model(); var books = new Backbone.Collection([warAndPeace]); books.contains(warAndPeace); // true books.isEmpty(); // false
For more advanced testing, you can use the every
and some
methods, which allow you to specify your own test logic. For instance, if you want to know whether any of the books in a Collection have more than a hundred pages, you can use the some
method, as follows:
var books = new Backbone.Collection([ {pages: 120, title: "Backbone Essentials 4: The Reckoning"}, {pages: 20, title: "Even More Ideas For Fake Book Titles"} ]); books.some(function(book) { return book.get('pages') > 100; }); // true books.every(function(book) { return...