Skip to content

Commit

Permalink
handle create failure with in-flight save
Browse files Browse the repository at this point in the history
pertains to #15

jus call me mario batalli 'cause i make spaghetti (code).
  • Loading branch information
aaronj1335 committed Feb 11, 2013
1 parent 495358f commit 2e71883
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
15 changes: 12 additions & 3 deletions js/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ define([

save: function() {
var changeArray, isBaseProp, request, subject, data, name, dfd,
cornerCaseDfd,
self = this,
args = Array.prototype.slice(0),
changes = self._changes,
Expand Down Expand Up @@ -331,9 +332,15 @@ define([
// can't make the 'update' call until we've got the ID the server
// gave us from the 'create'
if (!creating && !self._loaded) {
return _.last(inFlight).promise.then(function() {
return self.save.apply(self, args);
cornerCaseDfd = $.Deferred();
_.last(inFlight).promise.always(function() {
self.save.apply(self, args).then(function() {
cornerCaseDfd.resolve.apply(cornerCaseDfd, arguments);
}, function() {
cornerCaseDfd.reject.apply(cornerCaseDfd, arguments);
});
});
return cornerCaseDfd.promise();
}

inFlight.push({
Expand Down Expand Up @@ -419,7 +426,9 @@ define([
},

_creating: function() {
return !this._loaded && this._inFlight.save.length === 0;
return !this._loaded && !_.find(this._inFlight.save, function(req) {
return req.dfd.state() === 'pending';
});
},

// this translate something like 'foo.bar' into the corresponding Field
Expand Down
55 changes: 28 additions & 27 deletions js/tests/test_model_consistency.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,33 +426,34 @@ define([
});

// TODO: make this pass
// asyncTest('failing initial create', function() {
// setup({noCollection: true}).then(function() {
// var save1, save2, firstSaveCompleted,
// m = Example({required_field: 'foo'});
// Example.mockFailure(true).mockDelay(50);
// save1 = m.save();
// save1.then(function() {
// ok(false, 'first save should have failed');
// start();
// }, function() {
// ok(true, 'first save failed');
// firstSaveCompleted = true;
// });
// Example.mockFailure();
// equal(save1.state(), 'pending');
// save2 = m.save();
// save2.then(function() {
// ok(firstSaveCompleted, 'first save completed');
// ok(save1.state(), 'rejected');
// start();
// }, function() {
// ok(false, 'second save should have succeeded');
// start();
// });

// });
// });
asyncTest('failing initial create', function() {
setup({noCollection: true}).then(function() {
var save1, save2, firstSaveCompleted,
m = Example({required_field: 'foo'});
Example.mockFailure(true).mockDelay(50);
save1 = m.save();
save1.then(function() {
ok(false, 'first save should have failed');
start();
}, function() {
ok(true, 'first save failed');
firstSaveCompleted = true;
});
Example.mockFailure();
equal(save1.state(), 'pending');
m.set('required_field', 'foobar');
save2 = m.save();
save2.then(function() {
ok(firstSaveCompleted, 'first save completed');
ok(save1.state(), 'rejected');
start();
}, function() {
ok(false, 'second save should have succeeded');
start();
});

});
});

asyncTest('calling save on existing model with in flight update returns first dfd', function() {
setup().then(function(c) {
Expand Down

0 comments on commit 2e71883

Please sign in to comment.