我是Backbone的新手,所以答案很简单。
我有一个由多态模型组成的集合,所以当我获取时,我通过构造函数运行数据来实例化正确的模型类型:
Entities.Posts = Backbone.Collection.extend({
url: 'feed',
model: function(data) {
if (data.created_at != undefined) {
if (data.plantType != undefined) {
if (data.quantity != undefined) {
//it's a want
console.log('its a want jim');
return new Entities.Want(data);
} else {
//it's a plant
console.log('its a plant jim');
return new Entities.Plant(data);
}
} else {
//it's an offer
console.log('its an offer jim');
return new Entities.Offer(data);
}
} else {
//default type to prevent uncaught error
return new Entities.Post(data);
}
}
工作正常,每个人都很开心。直到我尝试从其视图中删除模型:
deletePlant: function(e) {
console.log('delete clicked');
this.model.destroy({success: function(model, response){
console.log('plant delete success');
},
error: function(model,response) {
console.log(response);
},
dataType: 'text/html'});
App.trigger('posts:list');
}
这会从服务器中删除模型并将其从集合中删除,但是从模型/ [:id]'中删除模型后,骨干网会发送' DELETE / feed&# 39;这毫无意义。服务器返回404,因为/ feed没有DELETE路由。当我销毁属于该集合的模型时,为什么要向集合URL发送删除?
感谢。
编辑:以下是给我提问的模型代码。
App.module('Entities', function(Entities, App) {
Entities.Plant = Backbone.Model.extend( {
urlRoot: '/plants'
});
Entities.Plants = Backbone.Collection.extend( {
url: 'plants',
model: Entities.Plant,
comparator: 'created_at'
});
});