diff --git a/addon/-private/system/model/internal-model.js b/addon/-private/system/model/internal-model.js index c5475eec1c5..1375ba5130e 100644 --- a/addon/-private/system/model/internal-model.js +++ b/addon/-private/system/model/internal-model.js @@ -104,13 +104,12 @@ const { @class InternalModel */ export default class InternalModel { - constructor(modelClass, id, store, data) { + constructor(modelName, id, store, data) { heimdall.increment(new_InternalModel); - this.modelClass = modelClass; this.id = id; this.store = store; this._data = data || new EmptyObject(); - this.modelName = modelClass.modelName; + this.modelName = modelName; this.dataHasInitialized = false; this._loadingPromise = null; this._recordArrays = undefined; @@ -122,6 +121,7 @@ export default class InternalModel { this.error = null; // caches for lazy getters + this._modelClass = null; this.__deferredTriggers = null; this._references = null; this._recordReference = null; @@ -131,6 +131,10 @@ export default class InternalModel { this.__implicitRelationships = null; } + get modelClass() { + return this._modelClass || (this._modelClass = this.store.modelFor(this.modelName)); + } + get type() { return this.modelClass; } diff --git a/addon/-private/system/store.js b/addon/-private/system/store.js index e862b283f56..bf28eae3ab6 100644 --- a/addon/-private/system/store.js +++ b/addon/-private/system/store.js @@ -2305,14 +2305,14 @@ Store = Service.extend({ let unknownAttributes = Object.keys(data.attributes || {}).filter((key) => { return !get(modelClass, 'fields').has(key); }); - let unknownAttributesMessage = `The payload for '${modelClass.modelName}' contains these unknown attributes: ${unknownAttributes}. Make sure they've been defined in your model.`; + let unknownAttributesMessage = `The payload for '${modelName}' contains these unknown attributes: ${unknownAttributes}. Make sure they've been defined in your model.`; warn(unknownAttributesMessage, unknownAttributes.length === 0, { id: 'ds.store.unknown-keys-in-payload' }); // Check unknown relationships let unknownRelationships = Object.keys(data.relationships || {}).filter((key) => { return !get(modelClass, 'fields').has(key); }); - let unknownRelationshipsMessage = `The payload for '${modelClass.modelName}' contains these unknown relationships: ${unknownRelationships}. Make sure they've been defined in your model.`; + let unknownRelationshipsMessage = `The payload for '${modelName}' contains these unknown relationships: ${unknownRelationships}. Make sure they've been defined in your model.`; warn(unknownRelationshipsMessage, unknownRelationships.length === 0, { id: 'ds.store.unknown-keys-in-payload' }); } }); @@ -2462,8 +2462,7 @@ Store = Service.extend({ // lookupFactory should really return an object that creates // instances with the injections applied - // TODO @runspired don't force internalModel to need modelClass up front - let internalModel = new InternalModel(recordMap.modelClass, id, this, data); + let internalModel = new InternalModel(modelName, id, this, data); // if we're creating an item, this process will be done // later, once the object has been persisted. @@ -2638,8 +2637,8 @@ function _commit(adapter, store, operation, snapshot) { let internalModel = snapshot._internalModel; let modelName = snapshot.modelName; let modelClass = store.modelFor(modelName); - assert(`You tried to update a record but you have no adapter (for ${modelClass.modelName})`, adapter); - assert(`You tried to update a record but your adapter (for ${modelClass.modelName}) does not implement '${operation}'`, typeof adapter[operation] === 'function'); + assert(`You tried to update a record but you have no adapter (for ${modelName})`, adapter); + assert(`You tried to update a record but your adapter (for ${modelName}) does not implement '${operation}'`, typeof adapter[operation] === 'function'); let promise = adapter[operation](store, modelClass, snapshot); let serializer = serializerForAdapter(store, adapter, modelName); let label = `DS: Extract and notify about ${operation} completion of ${internalModel}`; diff --git a/tests/unit/store/push-test.js b/tests/unit/store/push-test.js index eaa29a0c2e2..6d63fe5375e 100644 --- a/tests/unit/store/push-test.js +++ b/tests/unit/store/push-test.js @@ -731,6 +731,23 @@ test("_push returns an instance of InternalModel if an object is pushed", functi assert.notOk(pushResult.record, 'InternalModel is not materialized'); }); +test("_push does not require a modelName to resolve to a modelClass", function(assert) { + let originalCall = store.modelFor; + store.modelFor = () => { assert.notOk('modelFor was triggered as a result of a call to store._push'); }; + + run(function() { + store._push({ + data: { + id: 1, + type: 'person' + } + }); + }); + + store.modelFor = originalCall; + assert.ok('We made it'); +}); + test("_push returns an array of InternalModels if an array is pushed", function(assert) { let pushResult;