Trunk: challenge success with a special synchronization method

I have my own sync method in my backbone.js application. All my models call this method, but since I override success in this method, my success methods from individual models are no longer called. Here is what I mean - Below is my own synchronization method:

 app.customSync = function(method, model, options) { var success = options.success, error = options.error, customSuccess = function(resp, status, xhr) { //call original, trigger custom event if(con)console.log('in custom success'); success(resp, status, xhr); }, customError = function(resp, status, xhr) { if(con)console.log('in custom error'); error(resp, status, xhr); }; options.success = customSuccess; options.error = customError; Backbone.sync(method, model, options); }; Backbone.Model.prototype.sync = app.customSync; 

Here is an example that I am trying to call successful with saving the model:

 this.model.save({ success:function(model, response){ if(con)console.log('this is never called'); } }); 

Does anyone know how I can still set up synchronization with custom success methods and achieve success from my individual savings?

As a note, I tried calling success msuccess in model.save , but msuccess was undefined in user synchronization.

+4
source share
1 answer

The first argument to Model.save is the hash of the attributes you want to change, the parameters go second and hold success / error callbacks.

Try

 this.model.save({}, { success: function() { console.log('save success'); } }); 

And a script to see this at work http://jsfiddle.net/nikoshr/XwfTB/

+4
source

All Articles