Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHORE] Move _modelForMixin to Model package #6802

Merged
merged 2 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions packages/model/addon/-private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export { default as belongsTo } from './belongs-to';
export { default as hasMany } from './has-many';
export { default as Model } from './model';
export { default as Errors } from './errors';

export { default as _modelForMixin } from './system/model-for-mixin';
34 changes: 34 additions & 0 deletions packages/model/addon/-private/system/model-for-mixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Model from '../model';
import { getOwner } from '@ember/application';
type Store = import('@ember-data/store').default;

/*
In case someone defined a relationship to a mixin, for example:
```
let Comment = Model.extend({
owner: belongsTo('commentable'. { polymorphic: true })
});
let Commentable = Ember.Mixin.create({
comments: hasMany('comment')
});
```
we want to look up a Commentable class which has all the necessary
relationship metadata. Thus, we look up the mixin and create a mock
Model, so we can access the relationship CPs of the mixin (`comments`)
in this case
*/
export default function modelForMixin(store: Store, normalizedModelName: string): Model | null {
let owner = getOwner(store);
let MaybeMixin = owner.factoryFor(`mixin:${normalizedModelName}`);
let mixin = MaybeMixin && MaybeMixin.class;
if (mixin) {
let ModelForMixin = Model.extend(mixin);
ModelForMixin.reopenClass({
__isMixin: true,
__mixin: mixin,
});
//Cache the class as a model
owner.register('model:' + normalizedModelName, ModelForMixin);
}
return owner.factoryFor(`model:${normalizedModelName}`);
}
61 changes: 14 additions & 47 deletions packages/store/addon/-private/system/schema-definition-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import { getOwner } from '@ember/application';
import normalizeModelName from './normalize-model-name';
import { RelationshipsSchema, AttributesSchema } from '../ts-interfaces/record-data-schemas';
import require from 'require';
import CoreStore from './core-store';
import { HAS_MODEL_PACKAGE } from '@ember-data/private-build-infra';

type Model = import('@ember-data/model').default;

let _Model;
function getModel() {
if (HAS_MODEL_PACKAGE) {
_Model = _Model || require('@ember-data/model').default;
}
return _Model;
type ModelForMixin = (store: Store, normalizedModelName: string) => Model | null;

let _modelForMixin: ModelForMixin;
if (HAS_MODEL_PACKAGE) {
let _found;
_modelForMixin = function() {
if (!_found) {
_found = require('@ember-data/model/-private')._modelForMixin;
}
return _found(...arguments);
};
}

export class DSModelSchemaDefinitionService {
Expand Down Expand Up @@ -83,13 +86,13 @@ export class DSModelSchemaDefinitionService {
* @param normalizedModelName already normalized modelName
* @return {*}
*/
export function getModelFactory(store: CoreStore, cache, normalizedModelName: string): Model | null {
export function getModelFactory(store: Store, cache, normalizedModelName: string): Model | null {
let factory = cache[normalizedModelName];

if (!factory) {
factory = _lookupModelFactory(store, normalizedModelName);

if (!factory) {
if (!factory && HAS_MODEL_PACKAGE) {
//Support looking up mixins as base types for polymorphic relationships
factory = _modelForMixin(store, normalizedModelName);
}
Expand All @@ -114,44 +117,8 @@ export function getModelFactory(store: CoreStore, cache, normalizedModelName: st
return factory;
}

export function _lookupModelFactory(store, normalizedModelName) {
export function _lookupModelFactory(store: Store, normalizedModelName: string): Model | null {
let owner = getOwner(store);

return owner.factoryFor(`model:${normalizedModelName}`);
}

/*
In case someone defined a relationship to a mixin, for example:
```
let Comment = Model.extend({
owner: belongsTo('commentable'. { polymorphic: true })
});
let Commentable = Ember.Mixin.create({
comments: hasMany('comment')
});
```
we want to look up a Commentable class which has all the necessary
relationship metadata. Thus, we look up the mixin and create a mock
Model, so we can access the relationship CPs of the mixin (`comments`)
in this case
*/
export function _modelForMixin(store, normalizedModelName) {
if (HAS_MODEL_PACKAGE) {
let owner = getOwner(store);
let MaybeMixin = owner.factoryFor(`mixin:${normalizedModelName}`);
let mixin = MaybeMixin && MaybeMixin.class;

if (mixin) {
let ModelForMixin = getModel().extend(mixin);
ModelForMixin.reopenClass({
__isMixin: true,
__mixin: mixin,
});

//Cache the class as a model
owner.register('model:' + normalizedModelName, ModelForMixin);
}

return _lookupModelFactory(store, normalizedModelName);
}
}