2011-06-23 62 views
1

我有以下Backbone集合。 我的問題是,當我做car.get(「品牌」),我得到undefined。覆蓋主幹集合添加錯誤

品牌屬性就在那裏。我知道,因爲當我做console.log(汽車)時,我可以看到它。就好像汽車對象上不存在.get屬性一樣。但那是不可能的,因爲我把我的汽車模型腳本放在我的Cars Collection腳本之前。

因此,杜佩因此失敗。甚至不知道這個。是否被調用。 但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); 
    } 
} 

回答

5

我的這個版本將類似於這樣:

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); 
    } 

}); 
+0

我想說謝謝你的幫助在這裏。我在異步調用中處於2級深度,我認爲事情並不像預期的那樣有效。我將在第1級工作流程中重新評估這一點。 –

+0

兩個深淺不一的似乎是一個糟糕的應用程序體系結構氣味。我在一個團隊中工作,構建了一個相當大的應用程序,我們從來沒有深入過兩層。 –

+0

不幸的是,我不控制返回的json數據。第二次異步調用取決於第一次返回的數據。有什麼建議麼? –