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] inject the Model specific _modelForMixin implementation into the store from the Model package #6800

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions packages/model/addon/-private/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { modelForMixin } from './system/model-for-mixin';

export { default as attr } from './attr';
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';

require('@ember-data/store').default.prototype._modelForMixin = modelForMixin;
18 changes: 18 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,18 @@
import Model from '../model';
import { getOwner } from '@ember/application';

export function modelForMixin(normalizedModelName: string): Model {
let owner = getOwner(this);
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}`);
}
24 changes: 24 additions & 0 deletions packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3514,6 +3514,30 @@ abstract class CoreStore extends Service {

updated.length = 0;
}

/**
In case someone defined a relationship to a mixin, for example:
```
let Comment = Model.extend({
owner: belongsTo('commentable'. { polymorphic: true })
});
let Commentable = 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

@method _modelForMixin
@protected
@param {String} normalizedModelName
@return {RecordInstance} recordInstance
*/
_modelForMixin(normalizedModelName: string): RecordInstance | undefined {
throw new Error('Method not implemented.');
}
}

defineProperty(
Expand Down
55 changes: 4 additions & 51 deletions packages/store/addon/-private/system/schema-definition-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,8 @@ import { get } from '@ember/object';
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;
}
import { RecordInstance } from '../ts-interfaces/record-instance';
Copy link
Contributor

Choose a reason for hiding this comment

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

RecordInstance is for the instantiated record instances, I believe based on usage below you are in need of ModelSchema or DSModelSchema


export class DSModelSchemaDefinitionService {
private _modelFactoryCache = Object.create(null);
Expand Down Expand Up @@ -83,15 +72,15 @@ export class DSModelSchemaDefinitionService {
* @param normalizedModelName already normalized modelName
* @return {*}
*/
export function getModelFactory(store: CoreStore, cache, normalizedModelName: string): Model | null {
export function getModelFactory(store: CoreStore, cache, normalizedModelName: string): RecordInstance | null {
let factory = cache[normalizedModelName];

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

if (!factory) {
//Support looking up mixins as base types for polymorphic relationships
factory = _modelForMixin(store, normalizedModelName);
factory = store._modelForMixin(normalizedModelName);
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of adding a private method to store and injecting it from the model package, we should use the HAS_MODEL_PACKAGE build flag to conditionally import a private function from that package.

let _modelForMixin;
if (HAS_MODEL_PACKAGE) {
 _modelForMixin = require('@ember-data/model/-private')._modelForMixin;
}

// ....

if (HAS_MODEL_PACKAGE) {
  factory = _modelForMixin(store, normalizedModelName);
}

possibly with a module scope cache for the method to avoid looking it up each time.

}

if (!factory) {
Expand All @@ -114,44 +103,8 @@ export function getModelFactory(store: CoreStore, cache, normalizedModelName: st
return factory;
}

export function _lookupModelFactory(store, normalizedModelName) {
export function _lookupModelFactory(store: CoreStore, normalizedModelName: string): RecordInstance | undefined {
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Store from '@ember-data/store';

export default class DefaultStore extends Store {
_modelForMixin() {}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Store from '@ember-data/store';
import Store from 'model-encapsulation-test-app/services/store';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

Expand Down