覆盖主干集合添加错误

Overriding Backbone Collection Add Error

本文关键字:添加 错误 集合 覆盖      更新时间:2023-09-26

我有以下主干集合我的问题是当我做car.get("品牌")时,我会被定义

品牌属性就在那里。我知道,因为当我做console.log(car)时,我可以看到它。就好像.get属性在car对象上不存在一样。但这是不可能的,因为我把我的汽车模型脚本放在我的汽车收藏脚本之前。

因此,isDupe失败了。甚至不确定是否有人在叫这个但是100%。get不存在帮助!

var Cars = Backbone.Collection.extend({
model: Car,
add: function(object) {
        var car = new Car(object);
        var isDupe = false;
        isDupe = this.any(function(_car) { 
            return _car.get("brand") === car.get("brand");
        });
        console.log("isDupe: " + isDupe);
        if (isDupe) {
            return false;
        }
        Backbone.Collection.prototype.add.call(this, car);
    }
}

我的版本与此类似:

Car = Backbone.Model.extend({
  eql: function(other) {
    return this.get('brand') == other.get('brand');
  }
});
CarList = Backbone.Collection.extend({
  model: Car,
  add: function(newCar) {
     isDupe = this.any(function(car) {
       return car.eql(newCar);
     });
     if (isDupe) return;
     Backbone.Collection.prototype.add.call(this, newCar);
  }
});