Skip to content

Commit

Permalink
[BUGFIX] Better Data Patch
Browse files Browse the repository at this point in the history
This version of the data patch doesn't rely on capturing the original
value. Instead it uses an alternate name that is defined by the AMD
loader internally, while replacing the main `require`.
  • Loading branch information
Chris Garrett committed Mar 25, 2020
1 parent c0f03e5 commit 4e8fe65
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@
"ember-addon": {
"configPath": "tests/dummy/config",
"after": [
"ember-source",
"ember-classic-decorator"
"ember-source"
]
}
}
23 changes: 7 additions & 16 deletions vendor/ember-decorators-polyfill/data-fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
);
}

let mainRequire = require;

function computedMacroWithOptionalParams(fn) {
return (...maybeDesc) =>
(isFieldDescriptor(maybeDesc)
Expand All @@ -28,16 +26,9 @@
let DS;

try {
DS = mainRequire('ember-data').default;
DS = window.requirejs('ember-data').default;
} catch (e) {
return mainRequire(moduleName);
}

if (window.require !== patchDataDecorators) {
// Something else patched, most likely ember-classic-decorator
// and since we're about to do things we shouldn't, get the original
// require back
mainRequire = window.require;
return window.requirejs(moduleName);
}

let {
Expand All @@ -54,19 +45,19 @@
DS.belongsTo = belongsTo;
DS.hasMany = hasMany;

if (mainRequire.entries['@ember-data/model/index']) {
if (window.requirejs.entries['@ember-data/model/index']) {
let newExports = Object.assign(
{},
mainRequire.entries['@ember-data/model/index'].module.exports,
window.requirejs.entries['@ember-data/model/index'].module.exports,
{ attr, belongsTo, hasMany }
);

mainRequire.entries['@ember-data/model/index'].module.exports = newExports;
window.requirejs.entries['@ember-data/model/index'].module.exports = newExports;
}

window.require = require = mainRequire;
window.require = require = window.requirejs;

return mainRequire(moduleName);
return window.requirejs(moduleName);
}
})();

0 comments on commit 4e8fe65

Please sign in to comment.