I'm facing an issue in my app where I need to delete a model from a collection using "this.model.destroy" in my view, but it triggers a 405 response and the response URL doesn't include the model's id. According to the Backbone documentation, the URL is supposed to be generated based on "Collection URL + Model ID". I have temporarily fixed the problem by manually passing in the URL to the destroy method, but I know this is not the ideal solution. I believe there should be a better way to handle this without resorting to a hack. The backend requires an ID when sending a DELETE request. What would be the best practice to achieve this using Backbone?
My.Model._entity = Backbone.Model.extend({
initialize: function(options) {
if (options.created && typeof options.created === 'string') {
this.set('created', new Date(options.created));
}
if (options.modified && typeof options.modified === 'string') {
this.set('modified', new Date(options.modified));
}
},
defaults: function() {
return {
created: new Date(),
modified: new Date()
};
}
});
My.Model.cartItem = My.Model._entity.extend({
defaults: function () {
var _def = My.Model.cartItem.__super__.defaults.apply(this, arguments);
return _.defaults(_def, {
description: "",
title: "",
image: "",
price: 0,
quantity: 0,
itemId: ''
});
},
url: '/checkout/item'
});
My.Collection.CartItem = Backbone.Collection.extend({
model: My.Model.cartItem,
url: '/checkout/item'
});