Skip to content

Commit b75a229

Browse files
committed
Update rollback methods, not to behave like undo
Fix behavior for rollbackAttributes. The intent is to rollback to the value fetched from the persistence layer; not to stash an attribute’s value on every call to `.set()`. - Fix failing tests for previousAttributes and rollbackAttributes - Update `attr()` `set()` method to only assign to `.previous` once
1 parent d1641c1 commit b75a229

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

addon/utils/attr.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ export default function attr(type = 'any', mutable = true) {
7171
this.set('attributes.' + key, value);
7272
if (!this.get('isNew')) {
7373
this._attributes[key] = this._attributes[key] || {};
74+
if (this._attributes[key].previous === undefined) {
75+
this._attributes[key].previous = lastValue;
76+
}
7477
this._attributes[key].changed = value;
75-
this._attributes[key].previous = lastValue;
76-
const service = this.get('service');
78+
let service = this.get('service');
7779
if (service) {
7880
service.trigger('attributeChanged', this);
7981
}

tests/unit/models/resource-test.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ test('#rollbackAttributes resets attributes based on #previousAttributes', funct
208208

209209
test('#rollbackRelationships resets relationships', function(assert) {
210210
let post = createPostWithRelationships.call(this);
211-
let ogAuthorId = post.get('relationships.author.data.id');
211+
const ogAuthorId = post.get('relationships.author.data.id');
212212
let relationships = post.get('relationships');
213213

214214
post.addRelationship('author', '5');
215-
assert.notEqual(relationships.author.id, ogAuthorId, 'author changed');
215+
assert.notEqual(relationships.author.data.id, ogAuthorId, 'author changed');
216216

217217
assert.equal(relationships.comments.data.length, 1, 'one comment');
218218
post.removeRelationships('comments', ['3']);
@@ -221,6 +221,19 @@ test('#rollbackRelationships resets relationships', function(assert) {
221221
let changes = post.get('changedRelationships');
222222
assert.equal(changes.length, 2, 'two relationships were changed');
223223

224+
post.addRelationship('author', '6');
225+
assert.equal(relationships.author.data.id, 6, 'author changed');
226+
post.addRelationships('comments', ['4', '5']);
227+
assert.equal(relationships.comments.data.length, 2, 'two comments added');
228+
229+
changes = post.get('changedRelationships');
230+
assert.equal(changes.length, 2, 'two relationships were changed');
231+
232+
post.removeRelationship('author');
233+
assert.equal(relationships.author.id, null, 'no author');
234+
changes = post.get('changedRelationships');
235+
assert.equal(changes.length, 2, 'two relationships were changed');
236+
224237
post.rollbackRelationships();
225238

226239
changes = post.get('changedRelationships');

0 commit comments

Comments
 (0)