Skip to content

Commit

Permalink
Ensure async belongsTo ember-data relationships report correct dirty/…
Browse files Browse the repository at this point in the history
…pristine status (#608)

* Ensure async ember-data relationships report correct dirty/pristine status

* add test

* lint fix

* bump ember-cli-babel
  • Loading branch information
snewcomer authored Jul 25, 2021
1 parent 31c508d commit 98ee70c
Show file tree
Hide file tree
Showing 5 changed files with 1,299 additions and 536 deletions.
10 changes: 9 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import mergeDeep from './utils/merge-deep';
import isObject from './utils/is-object';
import { tracked } from '@glimmer/tracking';
import { get as safeGet, set as safeSet } from '@ember/object';
import { macroCondition, dependencySatisfies, importSync } from '@embroider/macros';

const CHANGES = '_changes';
const PREVIOUS_CONTENT = '_previousContent';
Expand All @@ -32,6 +33,11 @@ function maybeUnwrapProxy(o) {
return isProxy(o) ? maybeUnwrapProxy(safeGet(o, 'content')) : o;
}

let Model;
if (macroCondition(dependencySatisfies('ember-data', '*'))) {
Model = importSync('@ember-data/model').default;
}

export class EmberChangeset extends BufferedChangeset {
@tracked _changes;
@tracked _errors;
Expand All @@ -48,8 +54,10 @@ export class EmberChangeset extends BufferedChangeset {
getDeep = safeGet;
mergeDeep = mergeDeep;

// override base class
safeGet(obj, key) {
if (Model && obj.relationshipFor?.(key)?.meta?.kind == 'belongsTo') {
return obj.belongsTo(key).value();
}
return safeGet(obj, key);
}
safeSet(obj, key, value) {
Expand Down
Loading

0 comments on commit 98ee70c

Please sign in to comment.