-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
export class DSModelSchemaDefinitionService { | ||
private _modelFactoryCache = Object.create(null); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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) { | ||
|
@@ -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); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/unpublished-model-encapsulation-test-app/app/services/store.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/unpublished-model-encapsulation-test-app/tests/integration/model-for-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ofModelSchema
orDSModelSchema