Actual for backbone 0.9.9
If you need to process data before Bacbone.Model.save() sends data to server:
1) Change data after validating in toJSON() overwritten method:
somewhere in code:
in Model class:
2) Or simply pass 2 sets of data to save():
If you need to process data before Bacbone.Model.save() sends data to server:
1) Change data after validating in toJSON() overwritten method:
somewhere in code:
this.save({ password: param.password , password_new: param.password_new , password_confirmation: param.password_confirmation }, { success: successHandler, error: errorHandler });
in Model class:
toJSON: function(options) { // do the same but with processing return this.encode(Backbone.Model.prototype.toJSON.call(this, options)); }, encode: function(attrs){ // encode passwords and remove confirmation attrs.password = $.md5(attrs.password); attrs.password_new = $.md5(attrs.password_new); delete attrs.password_confirmation; return attrs; },
2) Or simply pass 2 sets of data to save():
this.save({ password: param.password , password_new: param.password_new , password_confirmation: param.password_confirmation }, { success: successHandler, error: errorHandler, attrs:{ password: $.md5(param.password) , password_new: $.md5(param.password_new) } });First set of data will be validated and second used by sync in Backbone.sync:1437 (dev version) core function:
params.data = JSON.stringify(options.attrs || model.toJSON(options));