Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 844
Elmor Avatar asked May 10 '13 07:05

Elmor


1 Answers

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.

like image 84
Andrey Kuzmin Avatar answered Sep 27 '22 01:09

Andrey Kuzmin