Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone model.save() is sending PUT instead of POST

Tags:

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_", 
like image 854
D'Arcy Rail-Ip Avatar asked Nov 14 '12 17:11

D'Arcy Rail-Ip


People also ask

How do you save in backbone?

There are no save files in this game, you see. That means no save scumming, and no instant reloads when you do something ill-advised. You just… make the choices you make, and watch the consequences play out afterwards.

How does Backbone js work?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

Why use Backbone js?

BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.

What is Backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).


2 Answers

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:

If the model does not yet have an id, it is considered to be new.

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

like image 72
tonino.j Avatar answered Oct 07 '22 14:10

tonino.j


The above answers are correct in that if the model you are .save'ing has an id attribute backbone will do a PUT rather than a POST.

This behavior can be overridden simply by adding type: 'POST' to your save block:

var fooModel = new Backbone.Model({ id: 1});  fooModel.save(null, {   type: 'POST' }); 
like image 21
Lane Avatar answered Oct 07 '22 14:10

Lane