Skip to content

Commit

Permalink
final steps to ensuring modelClass is unneeded for _push
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Nov 18, 2016
1 parent 81161a8 commit c3dbc42
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
10 changes: 7 additions & 3 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
11 changes: 5 additions & 6 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
});
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}`;
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/store/push-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit c3dbc42

Please sign in to comment.