Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion addon/ember-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ const { modulePrefix, podModulePrefix } = config;
let DsModels, Models;
let DsSerializers, Serializers;

function _getAppInstance() {
const application = window.__app_for_mirage;
let appInstance = application.__deprecatedInstance__;
// If an appInstance wasn't found (such as when running the tests)
// then instantiate one manually, so we can use it to discover the
// ember-data models in a way that doesn't trigger deprecations.
if (!appInstance) {
application._buildDeprecatedInstance();
appInstance = application.__deprecatedInstance__;
}
return appInstance;
}

/**
* Get all ember data models under the app's namespaces
*
Expand Down Expand Up @@ -45,8 +58,19 @@ export function getDsModels() {
path.match(classicModelMatchRegex) || path.match(podModelMatchRegex);
if (matches && matches[1]) {
let modelName = matches[1];
let model = undefined;

// Use the appInstance to lookup the models if provided, to avoid triggering
// the ember-data:deprecate-early-static deprecation in ember-data
const appInstance = _getAppInstance();
if (appInstance) {
const modelNameExact = path.split('/models/')[1];
const store = appInstance.lookup('service:store');
model = store.modelFor(modelNameExact);
} else {
model = require(path, null, null, true).default;
}

let model = require(path, null, null, true).default;
if (isDsModel(model)) {
DsModels[modelName] = model;
}
Expand Down
2 changes: 2 additions & 0 deletions app/initializers/ember-cli-mirage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const { default: baseConfig, testConfig, makeServer } = config;
export default {
name: 'ember-cli-mirage',
initialize(application) {
window.__app_for_mirage = application;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kiwi-josh minor, I wonder if it might be safer to use a Symbol here, to ensure no one else uses __app_for_mirage even accidentally

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SergeAstapov yeah good call out - I've updated the code to use a Symbol instead. Let me know if theres a better way you would like it done.


if (baseConfig) {
application.register('mirage:base-config', baseConfig, {
instantiate: false,
Expand Down