Permalink
Browse files

Don't dirty records if value doesn't change

When setting an attribute on a record, the record
should only be marked dirty if the value being
set is different from the old value.

NB: Because we are comparing the serialized
values, so long as the values serialize to the
same string, they will be considered equal.
  • Loading branch information...
Tomhuda Katzdale
Tomhuda Katzdale committed Jun 26, 2012
1 parent db07b41 commit 3847ed317714556b834198bce60aefbd31fa83e3
Showing with 27 additions and 7 deletions.
  1. +15 −6 packages/ember-data/lib/system/model/attributes.js
  2. +12 −1 packages/ember-data/tests/unit/model_test.js
@@ -26,6 +26,17 @@ DS.Model.reopenClass({
}
});
function getAttr(record, options, key) {
var data = get(record, 'data');
var value = get(data, key);
if (value === undefined) {
value = options.defaultValue;
}
return value;
}
DS.attr = function(type, options) {
var transform = DS.attr.transforms[type];
Ember.assert("Could not find model attribute of type " + type, !!transform);
@@ -55,14 +66,12 @@ DS.attr = function(type, options) {
if (arguments.length === 2) {
value = transformTo(value);
this.setProperty(key, value);
} else {
data = get(this, 'data');
value = get(data, key);
if (value === undefined) {
value = options.defaultValue;
if (value !== getAttr(this, options, key)) {
this.setProperty(key, value);
}
} else {
value = getAttr(this, options, key);
}
return transformFrom(value);
@@ -7,7 +7,8 @@ module("DS.Model", {
store = DS.Store.create();
Person = DS.Model.extend({
name: DS.attr('string')
name: DS.attr('string'),
isDrugAddict: DS.attr('boolean')
});
},
@@ -24,6 +25,16 @@ test("can have a property set on it", function() {
equal(get(record, 'name'), 'bar', "property was set on the record");
});
test("setting a property on a record that has not changed does not cause it to become dirty", function() {
store.load(Person, { id: 1, name: "Peter", is_drug_addict: true });
var person = store.find(Person, 1);
equal(person.get('isDirty'), false, "precond - person record should not be dirty");
person.set('name', "Peter");
person.set('isDrugAddict', true);
equal(person.get('isDirty'), false, "record does not become dirty after setting property to old value");
});
test("a record reports its unique id via the `id` property", function() {
store.load(Person, { id: 1 });

0 comments on commit 3847ed3

Please sign in to comment.