Joined March 2013
·

Asa Ayers

Greenwood IN
·
·

Your example does not wrap the results in a new Backbone Collection as your title says. Instead it wraps it in an Underscore object, that happens to have a .size() that acts the same as a Backbone Collection's .size() would. To actually wrap the results in a backbone collection you'll need something like this:

var filtered = new Backbone.Collection(collection.filter(function(model) {
    return model.get('name').substr(0, 1) === 'b';
}));

For a more generic way you could add this function to your base object, to demonstrate with working code I'll patch it into Backbone.Collection.

Backbone.Collection.prototype.filteredCollection = function(iterator) {
    // If you're using a collection that extends Backbone.Collection
    // the constructor will give you the same type of object back.
    return new this.constructor(this.filter(iterator));
}
Achievements
44 Karma
518 Total ProTip Views