백본 컬렉션을 사용하여 서버에서 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'를 액세스 할 때에, 그것은에서 this.model.isNew()를 의미 대신 '_id'의 'ID를'반환 view는 서버에서 가져온 모든 레코드에 대해 'true'를 반환합니다. 따라서 서버에 항목을 삭제하거나 PUT 할 수 없습니다.
내가 (대신 PlaceModel 정의 내부의)이 같은 프로토 타입을 사용하여 idAttribute을 설정하지만 경우 :
Backbone.Model.prototype.idAttribute = "_id";
은 그럼 제대로 '_id'에 idAttribute를 매핑하고 모든 작동합니다. 무슨 일이 일어날까요?