我使用骨幹集合從服務器獲取mongodb集合。由於id存儲爲'_id',因此我使用idAttribute將其映射爲'_id'。獲取集合時未設置idAttribute
(function(){
var PlaceModel = Backbone.Model.extend({
idAttribute: "_id",
});
var PlaceCollection = Backbone.Collection.extend({
url: "http://localhost:9090/places",
initialize: function(options){
var that = this;
this.fetch({
success: function(){
console.log("Success!", that.toJSON());
},
error: function(){
console.log("Error");
}
});
}
});
var place = new PlaceCollection({model:PlaceModel});
}());
但後來,當我嘗試訪問模型的「idAttribute」時,它的時間來刪除條目,它返回,而不是「_id」身份證',從意味着this.model.isNew()對於從服務器獲取的所有記錄,視圖返回「true」。因此我不能刪除也不能向服務器放入一個條目。
但是如果我使用的原型是這樣(的PlaceModel定義裏面,而不是)設置idAttribute:
Backbone.Model.prototype.idAttribute = "_id";
然後正確idAttribute映射到「_id」,和一切正常。可能發生了什麼?