Skip to content

Commit

Permalink
[BUGFIX beta] rollbackAttributes() works after multiple failed saves
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit 6f9560e)

Conflicts:
	packages/ember-data/lib/system/model/states.js
  • Loading branch information
pangratz authored and bmac committed Oct 16, 2015
1 parent 8ed4a00 commit 0f3c4d1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
7 changes: 0 additions & 7 deletions packages/ember-data/lib/system/model/states.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import EmptyObject from "ember-data/system/empty-object";

import {
create,
keysFunc
Expand All @@ -8,7 +6,6 @@ import {
/**
@module ember-data
*/

var get = Ember.get;
/*
This file encapsulates the various states that a record can transition
Expand Down Expand Up @@ -359,10 +356,6 @@ var DirtyState = {

invokeLifecycleCallbacks: function(internalModel) {
internalModel.triggerLater('becameInvalid', internalModel);
},

exit: function(internalModel) {
internalModel._inFlightAttributes = new EmptyObject();
}
}
};
Expand Down
43 changes: 43 additions & 0 deletions packages/ember-data/tests/unit/model/rollback-attributes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,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;

Expand Down

0 comments on commit 0f3c4d1

Please sign in to comment.