这是过滤多个属性的骨干集合的好方法吗:
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
// i.e. ['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);
}
我对其进行了测试,它允许我以以下方式过滤集合:
filterParams = {brand:['ford','bmw','mazda'], color:black}
carCollection.filterData(filterParams);
它似乎有效,但我不知道是否有更好的方法来做到这一点。
我测试了Backbone.Collection.where()
方法,但如果我想说它不起作用brand: ['bmw','mazda','ford']