Elmor
Elmor

Reputation: 4895

backbone.js set callback on before save?

I was asked to remove couple of attributes from a backbone model (which was optional) where they exists. My first intent was to place something like a before_save callback on a model. But i didn't find any information googling.
is it possible to do that on a backbone side?

Upvotes: 6

Views: 4236

Answers (1)

Andrey Kuzmin
Andrey Kuzmin

Reputation: 4479

Just override default Model.save and add your callback to it.

var MyModel = Backbone.Model.extend({

   save: function (key, val, options) {
     this.beforeSave(key, val, options);
     return Backbone.Model.prototype.save.call(this, key, val, options);
   },

   beforeSave: function (key, val, options) {

   }

})

If you want only to remove particular attributes from being sent to the server than you may override Model.toJSON method.

Upvotes: 13

Related Questions