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.
Using
model.unset('_id')
should work fine. My guess is that the error is thrown by achange
event listener, either in your code or some library code. In order to not trigger events you can use thesilent:true
option.However, if you simply want to force the
model.save()
method to perform aPOST
, 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 bePOST
ed) or existing (and should bePUT
). Overriding the method to always return true will make your model to bePOST
ed every time: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 ofattributes
) which is what is used to make theisNew()
determination.If you want to force a
POST
, you'll need to delete theid
property: