Skip to content

Commit

Permalink
A record becomes clean again if a prop is reset
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Sep 26, 2013
1 parent 3662a65 commit e1f96f0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/ember-data/lib/system/model/attributes.js
Expand Up @@ -109,10 +109,11 @@ DS.attr = function(type, options) {
options: options
};

return Ember.computed(function(key, value, oldValue) {
return Ember.computed(function(key, value) {
if (arguments.length > 1) {
Ember.assert("You may not set `id` as an attribute on your model. Please remove any lines that look like: `id: DS.attr('<type>')` from " + this.constructor.toString(), key !== 'id');
this.send('didSetProperty', { name: key, oldValue: this._attributes[key] || this._inFlightAttributes[key] || this._data[key], value: value });
var oldValue = this._attributes[key] || this._inFlightAttributes[key] || this._data[key];
this.send('didSetProperty', { name: key, oldValue: oldValue, originalValue: this._data[key], value: value });
this._attributes[key] = value;
return value;
} else if (hasValue(this, key)) {
Expand Down
22 changes: 19 additions & 3 deletions packages/ember-data/lib/system/model/states.js
Expand Up @@ -170,10 +170,14 @@ var hasDefinedProperties = function(object) {
};

var didSetProperty = function(record, context) {
if (context.value !== context.oldValue) {
if (context.value === context.originalValue) {
delete record._attributes[context.name];
record.send('propertyWasReset', context.name);
} else if (context.value !== context.oldValue) {
record.send('becomeDirty');
record.updateRecordArraysLater();
}

record.updateRecordArraysLater();
};

// Implementation notes:
Expand Down Expand Up @@ -231,10 +235,20 @@ var DirtyState = {
// This means that there are local pending changes, but they
// have not yet begun to be saved, and are not invalid.
uncommitted: {

// EVENTS
didSetProperty: didSetProperty,

propertyWasReset: function(record, name) {
var stillDirty = false;

for (var prop in record._attributes) {
stillDirty = true;
break;
}

if (!stillDirty) { record.send('rolledBack'); }
},

pushedData: Ember.K,

becomeDirty: Ember.K,
Expand Down Expand Up @@ -419,6 +433,8 @@ var RootState = {
// you out of the in-flight state.
rolledBack: Ember.K,

propertyWasReset: Ember.K,

// SUBSTATES

// A record begins its lifecycle in the `empty` state.
Expand Down

0 comments on commit e1f96f0

Please sign in to comment.