From 6f9560eff318db8f2577bf41f58a17a29cef4d8e Mon Sep 17 00:00:00 2001 From: pangratz Date: Wed, 14 Oct 2015 21:25:17 +0200 Subject: [PATCH] [BUGFIX beta] rollbackAttributes() works after multiple failed saves A rollback of dirty attributes didn't work correctly when a model is saved more than 1 time and the save fails. The issue is that the `exit` handler on the invalid state - which is called when the model is saved again and it is transitioned into the inFlight state - clears the `_inFlightAttributes` which unfortunately wipes all the data needed to rollback attributes. The exit handler has been implemented in the course of #1755, but the reported issue in that PR seems to be fixed ever since elsewhere in the code base, since the added test back test still is green. --- .../ember-data/lib/system/model/states.js | 5 --- .../unit/model/rollback-attributes-test.js | 43 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/packages/ember-data/lib/system/model/states.js b/packages/ember-data/lib/system/model/states.js index 732f14b1101..b71684c3e27 100644 --- a/packages/ember-data/lib/system/model/states.js +++ b/packages/ember-data/lib/system/model/states.js @@ -1,7 +1,6 @@ /** @module ember-data */ -import EmptyObject from "ember-data/system/empty-object"; var get = Ember.get; /* This file encapsulates the various states that a record can transition @@ -347,10 +346,6 @@ var DirtyState = { invokeLifecycleCallbacks: function(internalModel) { internalModel.triggerLater('becameInvalid', internalModel); - }, - - exit: function(internalModel) { - internalModel._inFlightAttributes = new EmptyObject(); } } }; diff --git a/packages/ember-data/tests/unit/model/rollback-attributes-test.js b/packages/ember-data/tests/unit/model/rollback-attributes-test.js index 76fe0ac7116..c658693a836 100644 --- a/packages/ember-data/tests/unit/model/rollback-attributes-test.js +++ b/packages/ember-data/tests/unit/model/rollback-attributes-test.js @@ -244,6 +244,49 @@ test("invalid new record's attributes can be rollbacked", function() { }); }); +test("invalid record's attributes can be rollbacked after multiple failed calls - #3677", function() { + var person; + + var adapter = DS.RESTAdapter.extend({ + ajax: function(url, type, hash) { + var error = new DS.InvalidError(); + return Ember.RSVP.reject(error); + } + }); + + env = setupStore({ person: Person, adapter: adapter }); + + run(function() { + person = env.store.push({ + data: { + type: 'person', + id: 1, + attributes: { + firstName: 'original name' + } + } + }); + + person.set('firstName', 'updated name'); + }); + + run(function() { + equal(person.get('firstName'), 'updated name', "precondition: firstName is changed"); + + person.save().then(null, async(function() { + equal(person.get('hasDirtyAttributes'), true, "has dirty attributes"); + equal(person.get('firstName'), 'updated name', "firstName is still changed"); + + return person.save(); + })).then(null, async(function() { + person.rollbackAttributes(); + + equal(person.get('hasDirtyAttributes'), false, "has no dirty attributes"); + equal(person.get('firstName'), 'original name', "after rollbackAttributes() firstName has the original value"); + })); + }); +}); + test("deleted record's attributes can be rollbacked", function() { var person, people;