Skip to content

Commit

Permalink
Merge pull request #3351 from bmac/hasDirtyAttributes
Browse files Browse the repository at this point in the history
Deprecate isDirty in favor of Model#hasDirtyAttributes
  • Loading branch information
igorT committed Jun 15, 2015
2 parents aaeb609 + 5493b0d commit cbdd832
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 85 deletions.
8 changes: 4 additions & 4 deletions packages/ember-data/lib/system/debug/debug-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ export default Ember.DataAdapter.extend({
getRecordFilterValues: function(record) {
return {
isNew: record.get('isNew'),
isModified: record.get('isDirty') && !record.get('isNew'),
isClean: !record.get('isDirty')
isModified: record.get('hasDirtyAttributes') && !record.get('isNew'),
isClean: !record.get('hasDirtyAttributes')
};
},

getRecordColor: function(record) {
var color = 'black';
if (record.get('isNew')) {
color = 'green';
} else if (record.get('isDirty')) {
} else if (record.get('hasDirtyAttributes')) {
color = 'blue';
}
return color;
Expand All @@ -106,7 +106,7 @@ export default Ember.DataAdapter.extend({
observeRecord: function(record, recordUpdated) {
var releaseMethods = Ember.A();
var self = this;
var keysToObserve = Ember.A(['id', 'isNew', 'isDirty']);
var keysToObserve = Ember.A(['id', 'isNew', 'hasDirtyAttributes']);

record.eachAttribute(function(key) {
keysToObserve.push(key);
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/lib/system/debug/debug-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Model.reopen({
},
{
name: 'Flags',
properties: ['isLoaded', 'isDirty', 'isSaving', 'isDeleted', 'isError', 'isNew', 'isValid']
properties: ['isLoaded', 'hasDirtyAttributes', 'isSaving', 'isDeleted', 'isError', 'isNew', 'isValid']
}
];

Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/lib/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ InternalModel.prototype = {
isEmpty: retrieveFromCurrentState('isEmpty'),
isLoading: retrieveFromCurrentState('isLoading'),
isLoaded: retrieveFromCurrentState('isLoaded'),
isDirty: retrieveFromCurrentState('isDirty'),
hasDirtyAttributes: retrieveFromCurrentState('hasDirtyAttributes'),
isSaving: retrieveFromCurrentState('isSaving'),
isDeleted: retrieveFromCurrentState('isDeleted'),
isNew: retrieveFromCurrentState('isNew'),
Expand Down
32 changes: 31 additions & 1 deletion packages/ember-data/lib/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,38 @@ var Model = Ember.Object.extend(Ember.Evented, {
@property isDirty
@type {Boolean}
@readOnly
@deprecated
*/
isDirty: retrieveFromCurrentState,
isDirty: Ember.computed('currentState.isDirty', function() {
Ember.deprecate('DS.Model#isDirty has been deprecated please use hasDirtyAttributes instead');
return this.get('currentState.isDirty');
}),
/**
If this property is `true` the record is in the `dirty` state. The
record has local changes that have not yet been saved by the
adapter. This includes records that have been created (but not yet
saved) or deleted.
Example
```javascript
var record = store.createRecord('model');
record.get('hasDirtyAttributes'); // true
store.find('model', 1).then(function(model) {
model.get('hasDirtyAttributes'); // false
model.set('foo', 'some value');
model.get('hasDirtyAttributes'); // true
});
```
@property hasDirtyAttributes
@type {Boolean}
@readOnly
*/
hasDirtyAttributes: Ember.computed('currentState.isDirty', function() {
return this.get('currentState.isDirty');
}),
/**
If this property is `true` the record is in the `saving` state. A
record enters the saving state when `save` is called, but the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var hash = Ember.RSVP.hash;

function assertClean(promise) {
return promise.then(async(function(record) {
equal(record.get('isDirty'), false, "The record is now clean");
equal(record.get('hasDirtyAttributes'), false, "The record is now clean");
return record;
}));
}
Expand Down Expand Up @@ -134,8 +134,8 @@ test("An adapter can notify the store that records were updated by calling `didS
tom.set('name', "Michael Phelps");
yehuda.set('name', "Usain Bolt");

ok(tom.get('isDirty'), "tom is dirty");
ok(yehuda.get('isDirty'), "yehuda is dirty");
ok(tom.get('hasDirtyAttributes'), "tom is dirty");
ok(yehuda.get('hasDirtyAttributes'), "yehuda is dirty");

assertClean(tom.save()).then(async(function(record) {
equal(record, tom, "The record is correct");
Expand Down Expand Up @@ -188,8 +188,8 @@ test("An adapter can notify the store that a record was updated by calling `didS
people.tom.set('name', "Tom Dale");
people.yehuda.set('name', "Yehuda Katz");

ok(people.tom.get('isDirty'), "tom is dirty");
ok(people.yehuda.get('isDirty'), "yehuda is dirty");
ok(people.tom.get('hasDirtyAttributes'), "tom is dirty");
ok(people.yehuda.get('hasDirtyAttributes'), "yehuda is dirty");

assertClean(people.tom.save());
assertClean(people.yehuda.save());
Expand Down
24 changes: 12 additions & 12 deletions packages/ember-data/tests/integration/adapter/rest-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ test("create - an empty payload is a basic success if an id was specified", func
equal(passedVerb, "POST");
deepEqual(passedHash.data, { post: { id: "some-uuid", name: "The Parley Letter" } });

equal(post.get('isDirty'), false, "the post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(post.get('name'), "The Parley Letter", "the post was updated");
}));
});
Expand Down Expand Up @@ -212,7 +212,7 @@ test("create - a payload with a new ID and data applies the updates", function()
deepEqual(passedHash.data, { post: { name: "The Parley Letter" } });

equal(post.get('id'), "1", "the post has the updated ID");
equal(post.get('isDirty'), false, "the post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(post.get('name'), "Dat Parley Letter", "the post was updated");
}));
});
Expand All @@ -231,7 +231,7 @@ test("create - a payload with a new ID and data applies the updates (with legacy
deepEqual(passedHash.data, { post: { name: "The Parley Letter" } });

equal(post.get('id'), "1", "the post has the updated ID");
equal(post.get('isDirty'), false, "the post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(post.get('name'), "Dat Parley Letter", "the post was updated");
}));
});
Expand All @@ -257,7 +257,7 @@ test("create - findMany doesn't overwrite owner", function() {

run(function() {
comment.save().then(async(function(comment) {
equal(comment.get('isDirty'), false, "the post isn't dirty anymore");
equal(comment.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(comment.get('name'), "Dat Parley Letter", "the post was updated");
equal(comment.get('post'), post, "the post is still set");
}));
Expand Down Expand Up @@ -537,7 +537,7 @@ test("update - an empty payload is a basic success", function() {
equal(passedVerb, "PUT");
deepEqual(passedHash.data, { post: { name: "The Parley Letter" } });

equal(post.get('isDirty'), false, "the post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(post.get('name'), "The Parley Letter", "the post was updated");
}));
});
Expand Down Expand Up @@ -576,7 +576,7 @@ test("update - a payload with updates applies the updates", function() {
equal(passedVerb, "PUT");
deepEqual(passedHash.data, { post: { name: "The Parley Letter" } });

equal(post.get('isDirty'), false, "the post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(post.get('name'), "Dat Parley Letter", "the post was updated");
}));
});
Expand All @@ -596,7 +596,7 @@ test("update - a payload with updates applies the updates (with legacy singular
equal(passedVerb, "PUT");
deepEqual(passedHash.data, { post: { name: "The Parley Letter" } });

equal(post.get('isDirty'), false, "the post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(post.get('name'), "Dat Parley Letter", "the post was updated");
}));
});
Expand All @@ -615,7 +615,7 @@ test("update - a payload with sideloaded updates pushes the updates", function()
deepEqual(passedHash.data, { post: { name: "The Parley Letter" } });

equal(post.get('id'), "1", "the post has the updated ID");
equal(post.get('isDirty'), false, "the post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(post.get('name'), "Dat Parley Letter", "the post was updated");

var comment = store.peekRecord('comment', 1);
Expand All @@ -642,7 +642,7 @@ test("update - a payload with sideloaded updates pushes the updates", function()
equal(passedVerb, "PUT");
deepEqual(passedHash.data, { post: { name: "The Parley Letter" } });

equal(post.get('isDirty'), false, "the post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(post.get('name'), "Dat Parley Letter", "the post was updated");

var comment = store.peekRecord('comment', 1);
Expand Down Expand Up @@ -718,7 +718,7 @@ test("delete - an empty payload is a basic success", function() {
equal(passedVerb, "DELETE");
equal(passedHash, undefined);

equal(post.get('isDirty'), false, "the post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(post.get('isDeleted'), true, "the post is now deleted");
}));
});
Expand Down Expand Up @@ -757,7 +757,7 @@ test("delete - a payload with sideloaded updates pushes the updates", function()
equal(passedVerb, "DELETE");
equal(passedHash, undefined);

equal(post.get('isDirty'), false, "the post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the post isn't dirty anymore");
equal(post.get('isDeleted'), true, "the post is now deleted");

var comment = store.peekRecord('comment', 1);
Expand All @@ -780,7 +780,7 @@ test("delete - a payload with sidloaded updates pushes the updates when the orig
equal(passedVerb, "DELETE");
equal(passedHash, undefined);

equal(post.get('isDirty'), false, "the original post isn't dirty anymore");
equal(post.get('hasDirtyAttributes'), false, "the original post isn't dirty anymore");
equal(post.get('isDeleted'), true, "the original post is now deleted");

var newPost = store.peekRecord('post', 2);
Expand Down
42 changes: 21 additions & 21 deletions packages/ember-data/tests/integration/adapter/store-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ test("calling store.didSaveRecord can provide an optional hash", function() {
var tom = records.tom;
var yehuda = records.yehuda;

equal(get(tom, 'isDirty'), false, "the record should not be dirty");
equal(get(tom, 'hasDirtyAttributes'), false, "the record should not be dirty");
equal(get(tom, 'updatedAt'), "now", "the hash was updated");

equal(get(yehuda, 'isDirty'), false, "the record should not be dirty");
equal(get(yehuda, 'hasDirtyAttributes'), false, "the record should not be dirty");
equal(get(yehuda, 'updatedAt'), "now!", "the hash was updated");
}));
});
Expand Down Expand Up @@ -327,12 +327,12 @@ test("if an existing model is edited then deleted, deleteRecord is called on the
run(store, 'findRecord', 'person', 'deleted-record').then(async(function(tom) {
tom.set('name', "Tom Mothereffin' Dale");

equal(get(tom, 'isDirty'), true, "precond - record should be dirty after editing");
equal(get(tom, 'hasDirtyAttributes'), true, "precond - record should be dirty after editing");

tom.deleteRecord();
return tom.save();
})).then(async(function(tom) {
equal(get(tom, 'isDirty'), false, "record should not be dirty");
equal(get(tom, 'hasDirtyAttributes'), false, "record should not be dirty");
equal(get(tom, 'isDeleted'), true, "record should be considered deleted");
}));
});
Expand Down Expand Up @@ -397,7 +397,7 @@ test("if a created record is marked as invalid by the server, it enters an error
set(yehuda, 'name', "Brohuda Brokatz");

equal(get(yehuda, 'isValid'), true, "the record is no longer invalid after changing");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record has outstanding changes");

equal(get(yehuda, 'isNew'), true, "precond - record is still new");

Expand Down Expand Up @@ -438,7 +438,7 @@ test("allows errors on arbitrary properties on create", function() {
set(yehuda, 'name', "Brohuda Brokatz");

equal(get(yehuda, 'isValid'), false, "the record is still invalid as far as we know");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record has outstanding changes");

equal(get(yehuda, 'isNew'), true, "precond - record is still new");

Expand Down Expand Up @@ -477,23 +477,23 @@ test("if a created record is marked as invalid by the server, you can attempt th
equal(saveCount, 1, "The record has been saved once");
ok(reason.message.match("The backend rejected the commit because it was invalid"), "It should fail due to being invalid");
equal(get(yehuda, 'isValid'), false, "the record is invalid");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record has outstanding changes");
ok(get(yehuda, 'errors.name'), "The errors.name property exists");
equal(get(yehuda, 'isNew'), true, "precond - record is still new");
return yehuda.save();
})).then(null, async(function(reason) {
equal(saveCount, 2, "The record has been saved twice");
ok(reason.message.match("The backend rejected the commit because it was invalid"), "It should fail due to being invalid");
equal(get(yehuda, 'isValid'), false, "the record is still invalid");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record has outstanding changes");
ok(get(yehuda, 'errors.name'), "The errors.name property exists");
equal(get(yehuda, 'isNew'), true, "precond - record is still new");
set(yehuda, 'name', 'Brohuda Brokatz');
return yehuda.save();
})).then(async(function(person) {
equal(saveCount, 3, "The record has been saved thrice");
equal(get(yehuda, 'isValid'), true, "record is valid");
equal(get(yehuda, 'isDirty'), false, "record is not dirty");
equal(get(yehuda, 'hasDirtyAttributes'), false, "record is not dirty");
equal(get(yehuda, 'errors.isEmpty'), true, "record has no errors");
}));
});
Expand Down Expand Up @@ -536,24 +536,24 @@ test("if an updated record is marked as invalid by the server, it enters an erro
set(yehuda, 'name', "Yehuda Katz");
equal(get(yehuda, 'isValid'), true, "precond - the record is still valid as far as we know");

equal(get(yehuda, 'isDirty'), true, "the record is dirty");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is dirty");

return yehuda.save();
})).then(null, async(function(reason) {
equal(get(yehuda, 'isDirty'), true, "the record is still dirty");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is still dirty");
equal(get(yehuda, 'isValid'), false, "the record is invalid");

set(yehuda, 'updatedAt', true);
equal(get(yehuda, 'isValid'), false, "the record is still invalid");

set(yehuda, 'name', "Brohuda Brokatz");
equal(get(yehuda, 'isValid'), true, "the record is no longer invalid after changing");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record has outstanding changes");

return yehuda.save();
})).then(async(function(yehuda) {
equal(get(yehuda, 'isValid'), true, "record remains valid after committing");
equal(get(yehuda, 'isDirty'), false, "record is no longer new");
equal(get(yehuda, 'hasDirtyAttributes'), false, "record is no longer new");
}));
});
});
Expand All @@ -580,11 +580,11 @@ test("records can have errors on arbitrary properties after update", function()
set(yehuda, 'name', "Yehuda Katz");
equal(get(yehuda, 'isValid'), true, "precond - the record is still valid as far as we know");

equal(get(yehuda, 'isDirty'), true, "the record is dirty");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is dirty");

return yehuda.save();
})).then(null, async(function(reason) {
equal(get(yehuda, 'isDirty'), true, "the record is still dirty");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is still dirty");
equal(get(yehuda, 'isValid'), false, "the record is invalid");
ok(get(yehuda, 'errors.base'), "The errors.base property exists");
deepEqual(get(yehuda, 'errors').errorsFor('base'), [{ attribute: 'base', message: "is a generally unsavoury character" }]);
Expand All @@ -594,12 +594,12 @@ test("records can have errors on arbitrary properties after update", function()

set(yehuda, 'name', "Brohuda Brokatz");
equal(get(yehuda, 'isValid'), false, "the record is still invalid after changing (only server can know if it's now valid)");
equal(get(yehuda, 'isDirty'), true, "the record has outstanding changes");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record has outstanding changes");

return yehuda.save();
})).then(async(function(yehuda) {
equal(get(yehuda, 'isValid'), true, "record remains valid after committing");
equal(get(yehuda, 'isDirty'), false, "record is no longer new");
equal(get(yehuda, 'hasDirtyAttributes'), false, "record is no longer new");
ok(!get(yehuda, 'errors.base'), "The errors.base property does not exist");
deepEqual(get(yehuda, 'errors').errorsFor('base'), []);
}));
Expand Down Expand Up @@ -632,26 +632,26 @@ test("if an updated record is marked as invalid by the server, you can attempt t
set(yehuda, 'name', "Yehuda Katz");
equal(get(yehuda, 'isValid'), true, "precond - the record is still valid as far as we know");

equal(get(yehuda, 'isDirty'), true, "the record is dirty");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is dirty");

return yehuda.save();
})).then(null, async(function(reason) {
equal(saveCount, 1, "The record has been saved once");
ok(reason.message.match("The backend rejected the commit because it was invalid"), "It should fail due to being invalid");
equal(get(yehuda, 'isDirty'), true, "the record is still dirty");
equal(get(yehuda, 'hasDirtyAttributes'), true, "the record is still dirty");
equal(get(yehuda, 'isValid'), false, "the record is invalid");
return yehuda.save();
})).then(null, async(function(reason) {
equal(saveCount, 2, "The record has been saved twice");
ok(reason.message.match("The backend rejected the commit because it was invalid"), "It should fail due to being invalid");
equal(get(yehuda, 'isValid'), false, "record is still invalid");
equal(get(yehuda, 'isDirty'), true, "record is still dirty");
equal(get(yehuda, 'hasDirtyAttributes'), true, "record is still dirty");
set(yehuda, 'name', 'Brohuda Brokatz');
return yehuda.save();
})).then(async(function(person) {
equal(saveCount, 3, "The record has been saved thrice");
equal(get(yehuda, 'isValid'), true, "record is valid");
equal(get(yehuda, 'isDirty'), false, "record is not dirty");
equal(get(yehuda, 'hasDirtyAttributes'), false, "record is not dirty");
equal(get(yehuda, 'errors.isEmpty'), true, "record has no errors");
}));
});
Expand Down
Loading

0 comments on commit cbdd832

Please sign in to comment.