I call save using this:
console.log(this.model.isNew());
console.log(this.model);
this.model.save({}, {
success: function (model, response, options) {
console.log(response);
},
error: function (model, xhr, options) {
console.log(xhr.result.Errors);
}
});
The isNew()
returns false. But the output of this.model
has an ID of 0. (this.model.id is 0 as well)
My url is url: ROOTAREA + "/Expenses/Entry/",
Updating works fine, and uses PUT as expected.
Edit : here's part of my model:
defaults: function () {
return {
DocumentDate: "",
JobNo_: "",
PhaseCode: "",
WorkTypeCode: "",
Description: "",
Quantity: 0,
UnitCost: 0,
ExpenseCurrencyCode: "",
ReimbursementCurrencyCode: "",
UnitofMeasureCode: "DIEM",
LineNo_: 0
};
},
idAttribute: "LineNo_",
The above answers are correct in that if the
model
you are.save
'ing has anid
attribute backbone will do aPUT
rather than aPOST
.This behavior can be overridden simply by adding
type: 'POST'
to your save block:ID should not even exist for a new entry. The issue is in the part you didn't show - in the part where you instantiate, create and populate the model.
Here is a quote from the Backbone documentation:
It is clear from your code that you are assigning an id attribute. Your backend should be doing that. And since you are doing it on a client, backbone presumes it it not new, and uses
PUT
You can specify the ID in defaults, just make sure it's set to
null
(isNew will be set to true).In your case it must be