0

I would like to unset() the _id attribute from an instance of a model, to make a POST request using the save() model method.

But i get a Uncaught TypeError: Object [object Object] has no method 'call' backbone-min.js because of this line:

myModel.unset('_id');

I am using idAttribute: "_id" so i tried:

myModel.unset('id');

But it doesn't unset the _id attribute.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Ludo
  • 5,060
  • 15
  • 53
  • 85
  • Possible duplicate of [How to force a POST request when saving a model?](http://stackoverflow.com/questions/41090399/how-to-force-a-post-request-when-saving-a-model) – Emile Bergeron Dec 12 '16 at 01:37
  • Don't unset the `_id` attribute or `id` property to make a POST request. – Emile Bergeron Dec 12 '16 at 01:37
  • Possible duplicate of [What is the least ugly way to force Backbone.sync updates to use POST instead of PUT?](http://stackoverflow.com/q/8527694/1218980) – Emile Bergeron Dec 12 '16 at 02:01

2 Answers2

2

Using model.unset('_id') should work fine. My guess is that the error is thrown by a change event listener, either in your code or some library code. In order to not trigger events you can use the silent:true option.

However, if you simply want to force the model.save() method to perform a POST, you don't need to unset the _id attribute.

Instead override the model.isNew method. Backbone uses this to determine whether a model is new (and should be POSTed) or existing (and should be PUT). Overriding the method to always return true will make your model to be POSTed every time:

isNew: function() { return true; }
jevakallio
  • 35,324
  • 3
  • 105
  • 112
  • 1
    Careful with this one, from now on the model you set that as will always be new, no matter what. I you ever want to do a `PUT` it will require applying the old behavior. – Joe Mills Sep 10 '13 at 21:14
  • A little late, but I'm with Joe on this. Don't override `isNew` to force a POST request. [Here's 4 alternatives](http://stackoverflow.com/a/41091957/1218980). – Emile Bergeron Dec 12 '16 at 01:39
0

Backbone stores the attributes in an object called attributes within the model. The attribute _id, although representative of the ID of that model is not what is used to determine whether a model is new.

There is a property called id (sibling of attributes) which is what is used to make the isNew() determination.

If you want to force a POST, you'll need to delete the id property:

var id = model.id;
model.unset('_id');
delete model.id;
model.save(); // this will do a POST
anushr
  • 3,342
  • 3
  • 29
  • 50
  • I don't believe this is entirely accurate. Backbone does use the `model.id` field to determine if the model `isNew`, but using `unset` will make a `set` call that will clear the `id` when the `idAttribute` is cleared. See http://documentcloud.github.io/backbone/docs/backbone.html#section-45. – dule Jan 30 '14 at 20:50
  • @dule Backbone does indeed use `model.id` to do an `isNew` check -- which is what I said in my second paragraph. I was merely trying to clarify that `model.id` is different from `model.attributes.id` (which is what the getters use). – anushr Apr 30 '15 at 05:24