Search code examples
javascriptbackbone.jsbackbone-model

how to unset an _id backbone model attribute?


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.


Solution

  • 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; }