Backbone collection where clause with OR condition

前端 未结 2 1415
挽巷
挽巷 2021-02-04 14:58

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

相关标签:
2条回答
  • 2021-02-04 15:15
    Cars.filter(function(car) {
        return car.get("model") === 1998 ||
            car.get("color") === "Black" ||
            car.get("make") === "Honda";
    });
    
    0 讨论(0)
  • 2021-02-04 15:22

    I know that this an old post, but maybe this could be useful to somebody.

    I had a similar problem, but a little simplier, this how I solved it

    var ids=[1,2,3,4];
    var plans=this.collection.filter(function(plan){
                var rt=false;
                for(var i=0;i<this.whereOR.length;i++){
                    rt=rt||plan.get('id')==this.whereOR[i];
                }
                return rt;
            },{whereOR:ids});
    

    I think this could be adapted to solve the problem proposed like this:

    var search={model: 1998,color: 'Black',make: 'Honda'};
    Cars.filter(function(car){
                    var rt=false;
    
                    for(key in this.whereOR) {
                        rt=rt||car.get(key)==this.whereOR[key];
                    }
                    return rt;
                },{whereOR:search});)
    

    Hope this help someone!

    0 讨论(0)
提交回复
热议问题