Skip to content

Commit

Permalink
Rollback Relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
mmpestorich authored and mattraydub committed Dec 15, 2017
1 parent 0999f8a commit 42e6e56
Show file tree
Hide file tree
Showing 15 changed files with 1,155 additions and 45 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
102 changes: 98 additions & 4 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,12 @@ export default class InternalModel {
}
}

notifyHasManyRemoved(key, record) {
if (this.hasRecord) {
this._record.notifyHasManyRemoved(key, record);
}
}

notifyBelongsToChanged(key, record) {
if (this.hasRecord) {
this._record.notifyBelongsToChanged(key, record);
Expand All @@ -758,6 +764,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 (this.isNew()) {
this.removeCompletelyFromInverseRelationships();
}
if (this.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 @@ -772,7 +817,7 @@ export default class InternalModel {
}

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

if (this.isValid()) {
Expand All @@ -786,6 +831,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 @@ -886,15 +943,52 @@ 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 remove this record from any associated relationships.
It will add this record to the current state of each relationships'
inverse relationship.
@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.
@method removeCompletelyFromInverseRelationships
@private
*/
removeCompletelyFromInverseRelationships() {
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 @@ -711,6 +711,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 @@ -1044,8 +1064,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 @@ -1110,13 +1139,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 @@ -1927,6 +1973,7 @@ if (DEBUG) {
// the computed property.
let meta = value.meta();

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

0 comments on commit 42e6e56

Please sign in to comment.