Filter the core network by multiple attributes

This is a good approach to filtering the core network for several attributes:

filterData: function(params) { // store original data for clearing filters this.originalModels = this.models.slice(); for (var key in params) { var val = params[key]; // if we are dealing with multiple values in an array // ie ['ford','bmw','mazda'] if (typeof val === "object") { var union = []; for (var k in val) { var subval = val[k]; var matched = _.filter(this.models, function(house) { return house.get(key) == subval; }); union = union.concat(matched); } this.models = union; } else { var results = _.filter(this.models, function(house) { return house.get(key) == val; }); this.models = results; } } // end for return this.reset(this.models); }, clearFilters: function() { return this.reset(this.originalModels); } 

I tested it and it allows you to filter the collection as follows:

 filterParams = {brand:['ford','bmw','mazda'], color:black} carCollection.filterData(filterParams); 

It seems to work, but I don't know if there is a better way to do this.

I tested the Backbone.Collection.where() method, but it does not work if I want to say brand: ['bmw','mazda','ford']

+4
source share
2 answers

You should be able to use something like this:

 filterData: function(params) { // store original data for clearing filters this.originalModels = this.models.slice(); _.each(params, function(val, key){ if (typeof val !== 'object') val = [ val ]; this.models = _.filter(this.models, function(model){ return _.indexOf(val, model.get(key)) !== -1; }, this); }, this); return this.reset(this.models); } 
+7
source

The Backbone.js collections have access to several underscore.js methods, including filter ().

 carCollection.reset(carCollection.filter(function(car){ return $.inArray(car.brand,['ford','bmw','mazda']) && car.color === 'black'; })); 

Not tested, but this should work if you use jQuery. If not, you can create your own inArray () function

+1
source

All Articles