Skip to content

Commit

Permalink
v2.17.0
Browse files Browse the repository at this point in the history
+ Rollback Relationships (cherry picked from commit 42e6e56)
+ [BUGFIX] Fix store.createRecord with belongsTo when model has @each observer (cherry picked from commit c257bbb)
+ [BUGFIX emberjs#5274] fixes breaking test for bug emberjs#5274
+ Move initialize-store-service.js out of the instance-initializers directory. (cherry picked from commit f3e1e32) Fixes emberjs#5118
  • Loading branch information
mattraydub committed Dec 15, 2017
1 parent 01760fe commit 10285ba
Show file tree
Hide file tree
Showing 22 changed files with 1,334 additions and 79 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
119 changes: 101 additions & 18 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,6 @@ export default class InternalModel {
adapterError: this.error
};

if (typeof properties === 'object' && properties !== null) {
emberAssign(createOptions, properties);
}

if (setOwner) {
// ensure that `getOwner(this)` works inside a model instance
setOwner(createOptions, getOwner(this.store));
Expand All @@ -350,6 +346,7 @@ export default class InternalModel {
}

this._record = this.store.modelFactoryFor(this.modelName).create(createOptions);
this._record.setProperties(properties);

this._triggerDeferredTriggers();
heimdall.stop(token);
Expand Down Expand Up @@ -746,6 +743,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 +761,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 +814,7 @@ export default class InternalModel {
}

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

if (this.isValid()) {
Expand All @@ -786,6 +828,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,24 +940,55 @@ 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.
If `isNew` is true (default false), it will also completely reset all
relationships to an empty state as well.
@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
@param {Boolean} isNew whether to unload from the `isNew` perspective
@private
*/
removeFromInverseRelationships(isNew = false) {
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();
if (isNew === true) {
rel.clear();
}
rel.clear();
});

let implicitRelationships = this._implicitRelationships;
Expand All @@ -913,9 +998,7 @@ export default class InternalModel {
let rel = implicitRelationships[key];

rel.removeCompletelyFromInverse();
if (isNew === true) {
rel.clear();
}
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 10285ba

Please sign in to comment.