Skip to content

Instantly share code, notes, and snippets.

@JohnDMathis
Created August 8, 2012 15:49
Show Gist options
  • Save JohnDMathis/3296136 to your computer and use it in GitHub Desktop.
Save JohnDMathis/3296136 to your computer and use it in GitHub Desktop.
Backbone save success never fires
//This is my 'Save' handler in a view...
saveRequested: function () {
this.model.save({
success: function () {
put('saved'); <-- never gets here, even when response is 200/OK
this.close();
},
error: function () {
put('error');
}
});
}
//----------
//this is inside backbone.js, starting at line 375, inside the model.save() function:
var model = this;
var success = options.success; <-- contains my save callback, above
options.success = function(resp, status, xhr) {
done = true; <-- resp='OK'
var serverAttrs = model.parse(resp, xhr);
if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs);
if (!model.set(serverAttrs, options)) return false;
if (success) success(model, resp, options); <-- success is now undefined... but, model.attributes.success now holds my function.
model.trigger('sync', model, resp, options);
};
Questions-
Why is the 'success' var undefined inside the callback function? 'model' is not.
Adding the following line solves my problem, but why is it necessary?
if (model.attributes.success) model.attributes.success(model, resp, options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment