UpTheCreek
UpTheCreek

Reputation: 32381

.save() on existing model causes POST instead of PUT

I'm having some trouble saving changes to individual models in a collection. Models that have been loaded (via a collection .reset()) are issuing a POST (as if they were new) instead of the expected PUT.

Here's the approach I'm taking:

AppView

ChildView

Observations:

Can anyone tell me why this may be happening?

Upvotes: 12

Views: 8416

Answers (1)

Derick Bailey
Derick Bailey

Reputation: 72858

backbone used the .id property (not attribute) of the model to determine whether it should put or post, as shown here in the source code: https://github.com/documentcloud/backbone/blob/master/backbone.js#L344-346

if it's doing a post when saving an existing model, this means the .id property wasn't loaded correctly. even if a call to model.get("id") returns the right result, a call to model.id must return the right result for it to know that this is not a new model.

be sure you're model's id attribute is called id, or if it's not, be sure to set the idAttribute on your model:

MyModel = Backbone.Model.extend({
  idAttribute: "myCustomId"
});

Upvotes: 29

Related Questions