Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell backbone that the model is not new

I have an object that's also saved in the server and I'm creating a Backbone model from that object.

But when I save the model, it's doing a PUT request, which is not what I want. How to tell Backbone that the data is already in the server without doing a fetch?

like image 776
archmage Avatar asked Apr 11 '12 16:04

archmage


1 Answers

Backbone determines the newness of a model by checking if an id is set :

isNew model.isNew()

Has this model been saved to the server yet? If the model does not yet have an id, it is considered to be new.

And when you save a model,

  • if it is new, a POST request will be emitted,
  • if it is an update (an id has been set), a PUT request will be sent

Backbone Sync documentation


And as noted by @JayC in the comments :

If there's an issue that the id can't literally be id, you can use idAttribute to say which is the "identity" or key field.

like image 72
nikoshr Avatar answered Sep 20 '22 16:09

nikoshr