Backbone collection where clause with OR condition

aditya_gaur picture aditya_gaur · Jan 8, 2013 · Viewed 8.1k times · Source

I have searched for this for quite some time but could not get a way to have a where clause with or condition. For example if I have a collection Cars and I try to do the following:

Cars.where({
            model: 1998,
            color: 'Black',
            make: 'Honda' 
          })

So what the above will do is search for a car whose model is 1998 AND color is Black AND make is Honda.

But I require a way to get cars which have either of the three conditions true.

Answer

Lukas picture Lukas · Jan 8, 2013
Cars.filter(function(car) {
    return car.get("model") === 1998 ||
        car.get("color") === "Black" ||
        car.get("make") === "Honda";
});