documentcloud/backbone — Issue #1030: calling `save` with {wait: true} results in incorrect final attributes if the success callback is called during the sync
Author: jalada
Created: 2012-02-20 17:01:42+00:00
State: open

You might call the success callback immediately if you are saving to local storage, for example.

The current code assumes that the call to sync will be asynchronous, and that the success callback will not happen immediately before setting the old attributes back to the model.

Here's a test that fails:

test("`save` with `wait` results in correct attributes if success is called during sync", function() {
var changed = 0
var model = new Backbone.Model({x: 1, y: 2});
model.sync = function(method, model, options) {
options.success();
}
model.on("change:x", function() { changed++; });
model.save({x: 3}, {wait: true});
equal(model.get('x'), 3);
equal(changed, 1);
});

Sorry if this is intentional (it might be?).

I fixed this by changing the standard success callback to unset options.wait, but that might not be ideal (all the tests still pass, however)