Skip to content

Commit

Permalink
Rollback Relationships
Browse files Browse the repository at this point in the history
This commit:

1. Allows one to rollback belongsTo and hasMany relationships.
2. Reintroduces dirtyRecordFor*Change hooks on the adapter that allow
   one to customize when a record becomes dirty.
3. Added 'removeDeletedFromRelationshipsPriorToSave' flag to Adapter
   that allows one to opt back into the old deleted record from many
   array behavior (pre emberjs#3539).

Known issues:

1. Rolling back a hasMany relationship from the parent side of the
   relationship does not work (doing the same from the child side works
   fine). See test that is commented out below as well as the discussion
   at the end of emberjs#2881#issuecomment-204634262

   This was previously emberjs#2881 and is related to emberjs#3698
  • Loading branch information
mmpestorich committed Feb 4, 2018
1 parent c8236d0 commit 113fa9f
Show file tree
Hide file tree
Showing 16 changed files with 1,061 additions and 42 deletions.
2 changes: 1 addition & 1 deletion addon/-debug/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function instrument(method) {
@param {InternalModel} addedRecord record which
should be added/set for the relationship
*/
let assertPolymorphicType;
let assertPolymorphicType = () => {};

if (DEBUG) {
let checkPolymorphic = function checkPolymorphic(modelClass, addedModelClass) {
Expand Down
100 changes: 96 additions & 4 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,45 @@ export default class InternalModel {
}
}

rollback() {
let dirtyKeys;

if (this.hasChangedAttributes()) {
dirtyKeys = Object.keys(this._attributes);
this._attributes = null;
}

if (get(this, 'isError')) {
this._inFlightAttributes = null;
this.didCleanError();
}

const isNew = this.isNew();
const isDeleted = this.isDeleted();

if (isNew || isDeleted) {
if (isNew) {
this.removeCompletelyFromInverseRelationships(true);
}
if (isDeleted) {
this.store.recordArrayManager.recordWasLoaded(this);
this.addToInverseRelationships();
}
} else {
this.rollbackRelationships();
}

if (this.isValid()) {
this._inFlightAttributes = {};
}

this.send('rolledBack');

if (dirtyKeys && dirtyKeys.length > 0) {
this._record._notifyProperties(dirtyKeys);
}
}

rollbackAttributes() {
let dirtyKeys;
if (this.hasChangedAttributes()) {
Expand All @@ -785,7 +824,7 @@ export default class InternalModel {
}

if (this.isNew()) {
this.removeFromInverseRelationships();
this.removeCompletelyFromInverseRelationships(true);
}

if (this.isValid()) {
Expand All @@ -799,6 +838,18 @@ export default class InternalModel {
}
}

/**
This method will rollback this record's relationships to their canonical state.
@method rollbackRelationships
@private
*/
rollbackRelationships() {
let implicitRelationships = this._implicitRelationships;
this.eachRelationship((key) => this._relationships.get(key).rollback());
Object.keys(implicitRelationships).forEach((key) => implicitRelationships[key].rollback());
}

/*
@method transitionTo
@private
Expand Down Expand Up @@ -899,15 +950,56 @@ export default class InternalModel {
}

/*
This method should only be called by records in the `isNew()` state OR once the record
has been deleted and that deletion has been persisted.
This method should only be called when rolling back records in the
`isDeleted()` state.
It will add this record to the current state of each relationships'
inverse relationship.
It will remove this record from any associated relationships.
@method addToInverseRelationships
@private
*/
addToInverseRelationships() {
if (this.store.adapterFor(this.modelName).removeDeletedFromRelationshipsPriorToSave) {
let implicitRelationships = this._implicitRelationships;
this.eachRelationship((key) => this._relationships.get(key).addInternalModelsToInverse());
Object.keys(implicitRelationships).forEach((key) => implicitRelationships[key].addInternalModelsToInverse());
}
}

/*
This method should only be called by records just after to transitioning
to a deleted state.
It will remove this record from the current state of each relationships'
inverse relationship.
@method removeFromInverseRelationships
@private
*/
removeFromInverseRelationships() {
if (this.store.adapterFor(this.modelName).removeDeletedFromRelationshipsPriorToSave) {
let implicitRelationships = this._implicitRelationships;
this.eachRelationship((key) => this._relationships.get(key).removeInternalModelsFromInverse());
Object.keys(implicitRelationships).forEach((key) => implicitRelationships[key].removeInternalModelsFromInverse());
}
}

/*
This method should only be called by records in the `isNew()` state
or once the record has been deleted and that deletion has been persisted.
It will remove this record from both the canonical and current state of
each relationships' inverse relationship.
If `isNew` is true (default false), it will also completely reset all
relationships to an empty state as well.
@method removeCompletelyFromInverseRelationships
@param {Boolean} isNew whether to unload from the `isNew` perspective
@private
*/
removeCompletelyFromInverseRelationships(isNew = false) {
this._relationships.forEach((name, rel) => {
rel.removeCompletelyFromInverse();
rel.clear();
Expand Down
55 changes: 51 additions & 4 deletions addon/-private/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,26 @@ const Model = EmberObject.extend(Evented, {
},
*/

/**
If the model `isDirty` this function will discard any unsaved
changes. If the model `isNew` it will be removed from the store.
Example
```javascript
record.get('name'); // 'Untitled Document'
record.set('name', 'Doc 1');
record.get('name'); // 'Doc 1'
record.rollback();
record.get('name'); // 'Untitled Document'
```
@method rollback
*/
rollback() {
this._internalModel.rollback();
},

/**
If the model `hasDirtyAttributes` this function will discard any unsaved
changes. If the model `isNew` it will be removed from the store.
Expand Down Expand Up @@ -1043,8 +1063,17 @@ const Model = EmberObject.extend(Evented, {
},

notifyBelongsToChanged(key) {
this.notifyPropertyChange(key);
const relationship = this._internalModel._relationships.get(key);
this._internalModel.notifyPropertyChange(key);
this._internalModel.send('didSetProperty', {
key: key,
kind: 'belongsTo',
isRelationship: true,
originalValue: relationship.canonicalState,
value: relationship.inverseInternalModel
});
},

/**
Given a callback, iterates over each of the relationships in the model,
invoking the callback with the name of each relationship and its relationship
Expand Down Expand Up @@ -1109,13 +1138,30 @@ const Model = EmberObject.extend(Evented, {
return this.constructor.inverseFor(key, this.store);
},

notifyHasManyAdded(key) {
notifyHasManyAdded(key, internalModelAdded) {
//We need to notifyPropertyChange in the adding case because we need to make sure
//we fetch the newly added record in case it is unloaded
//TODO(Igor): Consider whether we could do this only if the record state is unloaded
const internalModel = this._internalModel;
internalModel.notifyPropertyChange(key);
internalModel.send('didSetProperty', {
key: key,
kind: 'hasMany',
isRelationship: true,
originalValue: internalModel._relationships.get(key).canonicalMembers,
added: internalModelAdded
});
},

//Goes away once hasMany is double promisified
this.notifyPropertyChange(key);
notifyHasManyRemoved(key, internalModelRemoved) {
const internalModel = this._internalModel;
internalModel.send('didSetProperty', {
key: key,
kind: 'hasMany',
isRelationship: true,
originalValue: internalModel._relationships.get(key).canonicalMembers,
removed: internalModelRemoved
});
},

eachAttribute(callback, binding) {
Expand Down Expand Up @@ -1907,6 +1953,7 @@ if (DEBUG) {
// the computed property.
let meta = value.meta();

meta.key = key;
meta.parentType = proto.constructor;
}
}
Expand Down
Loading

0 comments on commit 113fa9f

Please sign in to comment.