Skip to content

Commit

Permalink
Merge pull request #5541 from emberjs/proxy-fix-3.3
Browse files Browse the repository at this point in the history
[BUGFIX] backport fix for belongsTo proxy update
  • Loading branch information
rwjblue authored Jul 25, 2018
2 parents ce11414 + 531311e commit b7acb78
Show file tree
Hide file tree
Showing 14 changed files with 709 additions and 425 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = {
root: true,
parserOptions: {
ecmaVersion: 6,
ecmaVersion: 2017,
sourceType: 'module',
},
parser: 'babel-eslint',
Expand Down
2 changes: 1 addition & 1 deletion addon/-private/system/many-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default EmberObject.extend(MutableArray, Evented, {
if (isInitialized && diff.addedCount > 0) {
//notify only on additions
//TODO only notify if unloaded
this.relationship.notifyHasManyChanged();
this.relationship.notifyHasManyChange();
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,9 +858,9 @@ export default class InternalModel {
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion addon/-private/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ const Model = EmberObject.extend(Evented, {
};
},

notifyBelongsToChanged(key) {
notifyBelongsToChange(key) {
this.notifyPropertyChange(key);
},
/**
Expand Down
33 changes: 22 additions & 11 deletions addon/-private/system/relationships/state/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class BelongsToRelationship extends Relationship {
} else if (this.inverseInternalModel) {
this.removeInternalModel(this.inverseInternalModel);
}

this.setHasAnyRelationshipData(true);
this.setRelationshipIsStale(false);
this.setRelationshipIsEmpty(false);
Expand Down Expand Up @@ -73,7 +74,7 @@ export default class BelongsToRelationship extends Relationship {

inverseDidDematerialize() {
super.inverseDidDematerialize(this.inverseInternalModel);
this.notifyBelongsToChanged();
this.notifyBelongsToChange();
}

removeCompletelyFromOwn(internalModel) {
Expand All @@ -85,7 +86,7 @@ export default class BelongsToRelationship extends Relationship {

if (this.inverseInternalModel === internalModel) {
this.inverseInternalModel = null;
this.notifyBelongsToChanged();
this.notifyBelongsToChange();
}
}

Expand All @@ -105,7 +106,7 @@ export default class BelongsToRelationship extends Relationship {
if (this.inverseInternalModel !== this.canonicalState) {
this.inverseInternalModel = this.canonicalState;
this._promiseProxy = null;
this.notifyBelongsToChanged();
this.notifyBelongsToChange();
}

super.flushCanonical();
Expand All @@ -122,7 +123,7 @@ export default class BelongsToRelationship extends Relationship {

this.inverseInternalModel = internalModel;
super.addInternalModel(internalModel);
this.notifyBelongsToChanged();
this.notifyBelongsToChange();
}

setRecordPromise(belongsToPromise) {
Expand All @@ -143,18 +144,24 @@ export default class BelongsToRelationship extends Relationship {
this.inverseInternalModel = null;
this._promiseProxy = null;
super.removeInternalModelFromOwn(internalModel);
this.notifyBelongsToChanged();
this.notifyBelongsToChange();
}

removeAllInternalModelsFromOwn() {
super.removeAllInternalModelsFromOwn();
this.inverseInternalModel = null;
this._promiseProxy = null;
this.notifyBelongsToChanged();
this.notifyBelongsToChange();
}

notifyBelongsToChanged() {
this.internalModel.notifyBelongsToChanged(this.key);
notifyBelongsToChange() {
if (this._promiseProxy !== null) {
let iM = this.inverseInternalModel;

this._updateLoadingPromise(proxyRecord(iM), iM ? iM.getRecord() : null);
}

this.internalModel.notifyBelongsToChange(this.key);
}

removeCanonicalInternalModelFromOwn(internalModel) {
Expand Down Expand Up @@ -229,9 +236,7 @@ export default class BelongsToRelationship extends Relationship {

if (this.isAsync) {
if (this._promiseProxy === null) {
let promise = resolve(this.inverseInternalModel).then(internalModel => {
return internalModel ? internalModel.getRecord() : null;
});
let promise = proxyRecord(this.inverseInternalModel);
this._updateLoadingPromise(promise, record);
}

Expand Down Expand Up @@ -270,6 +275,12 @@ export default class BelongsToRelationship extends Relationship {
}
}

function proxyRecord(internalModel) {
return resolve(internalModel).then(resolvedInternalModel => {
return resolvedInternalModel ? resolvedInternalModel.getRecord() : null;
});
}

function handleCompletedFind(relationship, error) {
let internalModel = relationship.inverseInternalModel;

Expand Down
4 changes: 2 additions & 2 deletions addon/-private/system/relationships/state/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default class ManyRelationship extends Relationship {
}
this._removeInternalModelFromManyArray(this._retainedManyArray, inverseInternalModel);
}
this.notifyHasManyChanged();
this.notifyHasManyChange();
}

addInternalModel(internalModel, idx) {
Expand Down Expand Up @@ -385,7 +385,7 @@ export default class ManyRelationship extends Relationship {
}
}

notifyHasManyChanged() {
notifyHasManyChange() {
this.internalModel.notifyHasManyAdded(this.key);
}

Expand Down
25 changes: 21 additions & 4 deletions addon/-private/system/relationships/state/relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default class Relationship {
this.canonicalMembers = new OrderedSet();
this.store = store;
this.key = relationshipMeta.key;
this.kind = relationshipMeta.kind;
this.inverseKey = inverseKey;
this.internalModel = internalModel;
this.isAsync = typeof async === 'undefined' ? true : async;
Expand Down Expand Up @@ -508,10 +509,21 @@ export default class Relationship {

updateLink(link, initial) {
heimdall.increment(updateLink);
warn(`You pushed a record of type '${this.internalModel.modelName}' with a relationship '${this.key}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload.`, this.isAsync || this.hasAnyRelationshipData , {
id: 'ds.store.push-link-for-sync-relationship'
});
assert(`You have pushed a record of type '${this.internalModel.modelName}' with '${this.key}' as a link, but the value of that link is not a string.`, typeof link === 'string' || link === null);
warn(
`You pushed a record of type '${this.internalModel.modelName}' with a relationship '${
this.key
}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload. EmberData will treat this relationship as known-to-be-empty.`,
this.isAsync || this.hasAnyRelationshipData,
{
id: 'ds.store.push-link-for-sync-relationship',
}
);
assert(
`You have pushed a record of type '${this.internalModel.modelName}' with '${
this.key
}' as a link, but the value of that link is not a string.`,
typeof link === 'string' || link === null
);

this.link = link;
this.fetchPromise = null;
Expand Down Expand Up @@ -670,6 +682,11 @@ export default class Relationship {
this.updateData(payload.data, initial);
} else if (payload._partialData !== undefined) {
this.updateData(payload._partialData, initial);
} else if (this.isAsync === false) {
hasRelationshipDataProperty = true;
let data = this.kind === 'hasMany' ? [] : null;

this.updateData(data, initial);
}

if (payload.links && payload.links.related) {
Expand Down
12 changes: 9 additions & 3 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2920,9 +2920,15 @@ function setupRelationships(store, internalModel, data, modelNameToInverseMap) {

if (relationshipData.links) {
let isAsync = relationshipMeta.options && relationshipMeta.options.async !== false;
warn(`You pushed a record of type '${internalModel.type.modelName}' with a relationship '${relationshipName}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload.`, isAsync || relationshipData.data , {
id: 'ds.store.push-link-for-sync-relationship'
});
warn(
`You pushed a record of type '${
internalModel.modelName
}' with a relationship '${relationshipName}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload. EmberData will treat this relationship as known-to-be-empty.`,
isAsync || relationshipData.data,
{
id: 'ds.store.push-link-for-sync-relationship',
}
);
} else if (relationshipData.data) {
if (relationshipMeta.kind === 'belongsTo') {
assert(`A ${internalModel.type.modelName} record was pushed into the store with the value of ${relationshipName} being ${inspect(relationshipData.data)}, but ${relationshipName} is a belongsTo relationship so the value must not be an array. You should probably check your data payload or serializer.`, !Array.isArray(relationshipData.data));
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
"silent-error": "^1.0.0"
},
"devDependencies": {
"babel-plugin-debug-macros": "^0.1.7",
"@ember-decorators/babel-transforms": "^2.0.0",
"@ember-decorators/data": "^2.1.0",
"babel-eslint": "^8.0.0",
"babel-plugin-debug-macros": "^0.1.7",
"babel-plugin-transform-es2015-arrow-functions": "^6.22.0",
"babel-plugin-transform-es2015-classes": "^6.23.0",
"babel-plugin-transform-es2015-computed-properties": "^6.22.0",
Expand All @@ -80,13 +80,11 @@
"ember-cli-app-version": "^3.0.0",
"ember-cli-blueprint-test-helpers": "^0.18.3",
"ember-cli-dependency-checker": "^2.1.0",
"ember-cli-eslint": "1.3.0",
"ember-cli-htmlbars": "^2.0.1",
"ember-cli-htmlbars-inline-precompile": "^0.4.3",
"ember-cli-inject-live-reload": "^1.4.1",
"ember-cli-internal-test-helpers": "^0.8.1",
"ember-cli-pretender": "^1.0.1",
"ember-qunit": "^3.4.0",
"ember-cli-release": "^0.2.9",
"ember-cli-shims": "^1.0.2",
"ember-cli-sri": "^2.1.0",
Expand All @@ -99,6 +97,7 @@
"ember-load-initializers": "^0.6.0",
"ember-maybe-import-regenerator": "^0.1.6",
"ember-publisher": "0.0.7",
"ember-qunit": "^3.4.0",
"ember-qunit-assert-helpers": "^0.2.1",
"ember-resolver": "^4.1.0",
"ember-source": "~3.0.0",
Expand Down
Loading

0 comments on commit b7acb78

Please sign in to comment.