From 2396e4e2da84604c02af9ef153de1a3b28d0a494 Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Mon, 17 Oct 2016 17:44:54 -0700 Subject: [PATCH] type/typeClass => modelClass, type => modelName --- addon/-private/system/store.js | 204 +++++++++++------------ tests/unit/store/adapter-interop-test.js | 78 ++++----- 2 files changed, 140 insertions(+), 142 deletions(-) diff --git a/addon/-private/system/store.js b/addon/-private/system/store.js index baf9a0e3879..80b0f154d97 100644 --- a/addon/-private/system/store.js +++ b/addon/-private/system/store.js @@ -317,7 +317,7 @@ Store = Service.extend({ createRecord(modelName, inputProperties) { assert("You need to pass a model name to the store's createRecord method", isPresent(modelName)); assert(`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${Ember.inspect(modelName)}`, typeof modelName === 'string'); - var typeClass = this.modelFor(modelName); + var modelClass = this.modelFor(modelName); var properties = copy(inputProperties) || new EmptyObject(); // If the passed properties do not include a primary key, @@ -332,7 +332,7 @@ Store = Service.extend({ // Coerce ID to a string properties.id = coerceId(properties.id); - var internalModel = this.buildInternalModel(typeClass, properties.id); + var internalModel = this.buildInternalModel(modelClass, properties.id); var record = internalModel.getRecord(); // Move the record out of its initial `empty` state into @@ -431,7 +431,7 @@ Store = Service.extend({ // the public way to get a record by modelName and id. if (arguments.length === 1) { - assert('Using store.find(type) has been removed. Use store.findAll(type) to retrieve all records for a given type.'); + assert('Using store.find(modelName) has been removed. Use store.findAll(modelName) to retrieve all records for a given modelClass.'); } if (Ember.typeOf(id) === 'object') { @@ -439,7 +439,7 @@ Store = Service.extend({ } if (options) { - assert('Calling store.find(type, id, { preload: preload }) is no longer supported. Use store.findRecord(type, id, { preload: preload }) instead.'); + assert('Calling store.find(modelName, id, { preload: preload }) is no longer supported. Use store.findRecord(type, id, { preload: preload }) instead.'); } assert("You need to pass the model name and id to the store's find method", arguments.length === 2); @@ -450,10 +450,10 @@ Store = Service.extend({ }, /** - This method returns a record for a given type and id combination. + This method returns a record for a given modelName and id combination. The `findRecord` method will always resolve its promise with the same - object for a given type and `id`. + object for a given modelName and `id`. The `findRecord` method will always return a **promise** that will be resolved with the record. @@ -663,7 +663,7 @@ Store = Service.extend({ var fetchedInternalModel = this._findRecord(internalModel, options); - return promiseRecord(fetchedInternalModel, "DS: Store#findRecord " + internalModel.typeKey + " with id: " + get(internalModel, 'id')); + return promiseRecord(fetchedInternalModel, "DS: Store#findRecord " + internalModel.modelName + " with id: " + get(internalModel, 'id')); }, _findRecord(internalModel, options) { @@ -673,8 +673,8 @@ Store = Service.extend({ } var snapshot = internalModel.createSnapshot(options); - var typeClass = internalModel.type; - var adapter = this.adapterFor(typeClass.modelName); + var modelClass = internalModel.modelClass; + var adapter = this.adapterFor(modelClass.modelName); // Refetch the record if the adapter thinks the record is stale if (adapter.shouldReloadRecord(this, snapshot)) { @@ -703,7 +703,7 @@ Store = Service.extend({ var fetchedInternalModel = this._findEmptyInternalModel(internalModel, options); - return promiseRecord(fetchedInternalModel, "DS: Store#findRecord " + internalModel.typeKey + " with id: " + get(internalModel, 'id')); + return promiseRecord(fetchedInternalModel, "DS: Store#findRecord " + internalModel.modelName + " with id: " + get(internalModel, 'id')); }, _findEmptyInternalModel(internalModel, options) { @@ -753,14 +753,14 @@ Store = Service.extend({ */ // TODO rename this to have an underscore fetchRecord(internalModel, options) { - var typeClass = internalModel.type; + var modelClass = internalModel.modelClass; var id = internalModel.id; - var adapter = this.adapterFor(typeClass.modelName); + var adapter = this.adapterFor(modelClass.modelName); - assert("You tried to find a record but you have no adapter (for " + typeClass + ")", adapter); - assert("You tried to find a record but your adapter (for " + typeClass + ") does not implement 'findRecord'", typeof adapter.findRecord === 'function' || typeof adapter.find === 'function'); + assert("You tried to find a record but you have no adapter (for " + modelClass + ")", adapter); + assert("You tried to find a record but your adapter (for " + modelClass + ") does not implement 'findRecord'", typeof adapter.findRecord === 'function' || typeof adapter.find === 'function'); - var promise = _find(adapter, this, typeClass, id, internalModel, options); + var promise = _find(adapter, this, modelClass, id, internalModel, options); return promise; }, @@ -779,11 +779,11 @@ Store = Service.extend({ }, scheduleFetch(internalModel, options) { - var typeClass = internalModel.type; + var modelClass = internalModel.modelClass; if (internalModel._loadingPromise) { return internalModel._loadingPromise; } - var resolver = Ember.RSVP.defer('Fetching ' + typeClass + 'with id: ' + internalModel.id); + var resolver = Ember.RSVP.defer('Fetching ' + modelClass + 'with id: ' + internalModel.id); var pendingFetchItem = { record: internalModel, resolver: resolver, @@ -793,10 +793,10 @@ Store = Service.extend({ internalModel.loadingData(promise); - if (!this._pendingFetch.get(typeClass)) { - this._pendingFetch.set(typeClass, [pendingFetchItem]); + if (!this._pendingFetch.get(modelClass)) { + this._pendingFetch.set(modelClass, [pendingFetchItem]); } else { - this._pendingFetch.get(typeClass).push(pendingFetchItem); + this._pendingFetch.get(modelClass).push(pendingFetchItem); } emberRun.scheduleOnce('afterRender', this, this.flushAllPendingFetches); @@ -812,9 +812,9 @@ Store = Service.extend({ this._pendingFetch = Map.create(); }, - _flushPendingFetchForType(pendingFetchItems, typeClass) { + _flushPendingFetchForType(pendingFetchItems, modelClass) { var store = this; - var adapter = store.adapterFor(typeClass.modelName); + var adapter = store.adapterFor(modelClass.modelName); var shouldCoalesce = !!adapter.findMany && adapter.coalesceFindRequests; var records = Ember.A(pendingFetchItems).mapBy('record'); @@ -884,7 +884,7 @@ Store = Service.extend({ var requestedRecords = Ember.A(groupOfRecords); var ids = requestedRecords.mapBy('id'); if (ids.length > 1) { - _findMany(adapter, store, typeClass, ids, requestedRecords). + _findMany(adapter, store, modelClass, ids, requestedRecords). then(resolveFoundRecords). then(makeMissingRecordsRejector(requestedRecords)). then(null, makeRecordsRejector(requestedRecords)); @@ -932,13 +932,13 @@ Store = Service.extend({ ``` @method getReference - @param {String} type + @param {String} modelName @param {String|Integer} id @since 2.5.0 @return {RecordReference} */ - getReference: function(type, id) { - return this._internalModelForId(type, id).recordReference; + getReference(modelName, id) { + return this._internalModelForId(modelName, id).recordReference; }, /** @@ -986,7 +986,7 @@ Store = Service.extend({ @return {Promise} promise */ reloadRecord(internalModel) { - var modelName = internalModel.type.modelName; + var modelName = internalModel.modelClass.modelName; var adapter = this.adapterFor(modelName); var id = internalModel.id; @@ -1081,9 +1081,9 @@ Store = Service.extend({ @return {Promise} promise */ findHasMany(owner, link, relationship) { - var adapter = this.adapterFor(owner.type.modelName); + var adapter = this.adapterFor(owner.modelClass.modelName); - assert("You tried to load a hasMany relationship but you have no adapter (for " + owner.type + ")", adapter); + assert("You tried to load a hasMany relationship but you have no adapter (for " + owner.modelClass + ")", adapter); assert("You tried to load a hasMany relationship from a specified `link` in the original payload but your adapter does not implement `findHasMany`", typeof adapter.findHasMany === 'function'); return _findHasMany(adapter, this, owner, link, relationship); @@ -1098,9 +1098,9 @@ Store = Service.extend({ @return {Promise} promise */ findBelongsTo(owner, link, relationship) { - var adapter = this.adapterFor(owner.type.modelName); + var adapter = this.adapterFor(owner.modelClass.modelName); - assert("You tried to load a belongsTo relationship but you have no adapter (for " + owner.type + ")", adapter); + assert("You tried to load a belongsTo relationship but you have no adapter (for " + owner.modelClass + ")", adapter); assert("You tried to load a belongsTo relationship from a specified `link` in the original payload but your adapter does not implement `findBelongsTo`", typeof adapter.findBelongsTo === 'function'); return _findBelongsTo(adapter, this, owner, link, relationship); @@ -1164,16 +1164,16 @@ Store = Service.extend({ assert("You need to pass a model name to the store's query method", isPresent(modelName)); assert("You need to pass a query hash to the store's query method", query); assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string'); - var typeClass = this.modelFor(modelName); + var modelClass = this.modelFor(modelName); array = array || this.recordArrayManager - .createAdapterPopulatedRecordArray(typeClass, query); + .createAdapterPopulatedRecordArray(modelClass, query); var adapter = this.adapterFor(modelName); - assert("You tried to load a query but you have no adapter (for " + typeClass + ")", adapter); + assert("You tried to load a query but you have no adapter (for " + modelClass + ")", adapter); assert("You tried to load a query but your adapter does not implement `query`", typeof adapter.query === 'function'); - let pA = promiseArray(_query(adapter, this, typeClass, query, array)); + let pA = promiseArray(_query(adapter, this, modelClass, query, array)); instrument(() => { pA.finally(() => { heimdall.stop(token); }); }); @@ -1278,19 +1278,19 @@ Store = Service.extend({ assert("You need to pass a query hash to the store's queryRecord method", query); assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string'); - var typeClass = this.modelFor(modelName); + var modelClass = this.modelFor(modelName); var adapter = this.adapterFor(modelName); - assert("You tried to make a query but you have no adapter (for " + typeClass + ")", adapter); + assert("You tried to make a query but you have no adapter (for " + modelClass + ")", adapter); assert("You tried to make a query but your adapter does not implement `queryRecord`", typeof adapter.queryRecord === 'function'); - return promiseObject(_queryRecord(adapter, this, typeClass, query)); + return promiseObject(_queryRecord(adapter, this, modelClass, query)); }, /** `findAll` asks the adapter's `findAll` method to find the records for the - given type, and returns a promise which will resolve with all records of - this type present in the store, even if the adapter only returns a subset + given modelName, and returns a promise which will resolve with all records of + this modelClass present in the store, even if the adapter only returns a subset of them. ```app/routes/authors.js @@ -1415,7 +1415,7 @@ Store = Service.extend({ import MyCustomAdapter from './custom-adapter'; export default MyCustomAdapter.extend({ - findAll: function(store, type, sinceToken, snapshotRecordArray) { + findAll: function(store, modelClass, sinceToken, snapshotRecordArray) { if (snapshotRecordArray.adapterOptions.subscribe) { // ... } @@ -1481,9 +1481,9 @@ Store = Service.extend({ assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string'); let token = heimdall.start('store.findAll'); - var typeClass = this.modelFor(modelName); + var modelClass = this.modelFor(modelName); - let fetch = this._fetchAll(typeClass, this.peekAll(modelName), options); + let fetch = this._fetchAll(modelClass, this.peekAll(modelName), options); instrument(() => { fetch.finally(() => { heimdall.stop(token); }); @@ -1495,28 +1495,28 @@ Store = Service.extend({ /** @method _fetchAll @private - @param {DS.Model} typeClass + @param {DS.Model} modelClass @param {DS.RecordArray} array @return {Promise} promise */ - _fetchAll(typeClass, array, options) { + _fetchAll(modelClass, array, options) { options = options || {}; - var adapter = this.adapterFor(typeClass.modelName); - var sinceToken = this.typeMapFor(typeClass).metadata.since; + var adapter = this.adapterFor(modelClass.modelName); + var sinceToken = this.typeMapFor(modelClass).metadata.since; - assert("You tried to load all records but you have no adapter (for " + typeClass + ")", adapter); + assert("You tried to load all records but you have no adapter (for " + modelClass + ")", adapter); assert("You tried to load all records but your adapter does not implement `findAll`", typeof adapter.findAll === 'function'); if (options.reload) { set(array, 'isUpdating', true); - return promiseArray(_findAll(adapter, this, typeClass, sinceToken, options)); + return promiseArray(_findAll(adapter, this, modelClass, sinceToken, options)); } var snapshotArray = array.createSnapshot(options); if (adapter.shouldReloadAll(this, snapshotArray)) { set(array, 'isUpdating', true); - return promiseArray(_findAll(adapter, this, typeClass, sinceToken, options)); + return promiseArray(_findAll(adapter, this, modelClass, sinceToken, options)); } if (options.backgroundReload === false) { @@ -1525,7 +1525,7 @@ Store = Service.extend({ if (options.backgroundReload || adapter.shouldBackgroundReloadAll(this, snapshotArray)) { set(array, 'isUpdating', true); - _findAll(adapter, this, typeClass, sinceToken, options); + _findAll(adapter, this, modelClass, sinceToken, options); } return promiseArray(Promise.resolve(array)); @@ -1533,26 +1533,26 @@ Store = Service.extend({ /** @method didUpdateAll - @param {DS.Model} typeClass + @param {DS.Model} modelClass @private */ - didUpdateAll(typeClass) { + didUpdateAll(modelClass) { heimdall.increment(didUpdateAll); - var liveRecordArray = this.recordArrayManager.liveRecordArrayFor(typeClass); + var liveRecordArray = this.recordArrayManager.liveRecordArrayFor(modelClass); set(liveRecordArray, 'isUpdating', false); }, /** This method returns a filtered array that contains all of the - known records for a given type in the store. + known records for a given modelClass in the store. Note that because it's just a filter, the result will contain any - locally created records of the type, however, it will not make a + locally created records of the modelClass, however, it will not make a request to the backend to retrieve additional records. If you would like to request all the records from the backend please use [store.findAll](#method_findAll). - Also note that multiple calls to `peekAll` for a given type will always + Also note that multiple calls to `peekAll` for a given modelClass will always return the same `RecordArray`. Example @@ -1570,10 +1570,10 @@ Store = Service.extend({ heimdall.increment(peekAll); assert("You need to pass a model name to the store's peekAll method", isPresent(modelName)); assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string'); - var typeClass = this.modelFor(modelName); + var modelClass = this.modelFor(modelName); - var liveRecordArray = this.recordArrayManager.liveRecordArrayFor(typeClass); - this.recordArrayManager.populateLiveRecordArray(liveRecordArray, typeClass); + var liveRecordArray = this.recordArrayManager.liveRecordArrayFor(modelClass); + this.recordArrayManager.populateLiveRecordArray(liveRecordArray, modelClass); return liveRecordArray; }, @@ -1581,7 +1581,7 @@ Store = Service.extend({ /** This method unloads all records in the store. - Optionally you can pass a type which unload all records for a given type. + Optionally you can pass a modelName which unload all records for a given modelClass. ```javascript store.unloadAll(); @@ -1620,7 +1620,7 @@ Store = Service.extend({ }, /** - Takes a type and filter function, and returns a live RecordArray that + Takes a modelName and filter function, and returns a live RecordArray that remains up to date as new records are loaded into the store or created locally. @@ -1636,7 +1636,7 @@ Store = Service.extend({ }); ``` - The filter function is called once on all records for the type when + The filter function is called once on all records for the modelName when it is created, and then once on each newly loaded or created record. If any of a record's properties change, or if it changes state, the @@ -1743,10 +1743,10 @@ Store = Service.extend({ @method dataWasUpdated @private - @param {Class} type + @param {Class} modelClass @param {InternalModel} internalModel */ - dataWasUpdated(type, internalModel) { + dataWasUpdated(modelClass, internalModel) { this.recordArrayManager.recordDidChange(internalModel); }, @@ -1792,7 +1792,7 @@ Store = Service.extend({ var snapshot = pendingItem.snapshot; var resolver = pendingItem.resolver; var record = snapshot._internalModel; - var adapter = this.adapterFor(record.type.modelName); + var adapter = this.adapterFor(record.modelClass.modelName); var operation; if (get(record, 'currentState.stateName') === 'root.deleted.saved') { @@ -1895,18 +1895,18 @@ Store = Service.extend({ return; } - this.typeMapFor(internalModel.type).idToRecord[id] = internalModel; + internalModel.schema.recordMap.idToRecord[id] = internalModel; internalModel.setId(id); }, /** - Returns a map of IDs to client IDs for a given type. + Returns a map of IDs to client IDs for a given modelClass. @method typeMapFor @private @param {DS.Model} modelClass - @return {Object} typeMap + @return {Object} recordMap */ typeMapFor(modelClass) { heimdall.increment(typeMapFor); @@ -1955,7 +1955,6 @@ Store = Service.extend({ @method _load @private - @param {(String|DS.Model)} type @param {Object} data */ _load(data) { @@ -2049,7 +2048,7 @@ Store = Service.extend({ }, /** - Push some data for a given type into the store. + Push some data into the store. This method expects normalized [JSON API](http://jsonapi.org/) document. This means you have to follow [JSON API specification](http://jsonapi.org/format/) with few minor adjustments: - record's `type` should always be in singular, dasherized form @@ -2232,8 +2231,8 @@ Store = Service.extend({ return record; }, - _hasModelFor(type) { - return !!getOwner(this)._lookupFactory(`model:${type}`); + _hasModelFor(modelName) { + return !!getOwner(this)._lookupFactory(`model:${modelName}`); }, _pushInternalModel(data) { @@ -2247,20 +2246,20 @@ Store = Service.extend({ // contains unknown attributes or relationships, log a warning. if (Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS) { - let type = this.modelFor(modelName); + let modelClass = this.modelFor(modelName); // Check unknown attributes let unknownAttributes = Object.keys(data.attributes || {}).filter((key) => { - return !get(type, 'fields').has(key); + return !get(modelClass, 'fields').has(key); }); - let unknownAttributesMessage = `The payload for '${type.modelName}' contains these unknown attributes: ${unknownAttributes}. Make sure they've been defined in your model.`; + let unknownAttributesMessage = `The payload for '${modelClass.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(type, 'fields').has(key); + return !get(modelClass, 'fields').has(key); }); - let unknownRelationshipsMessage = `The payload for '${type.modelName}' contains these unknown relationships: ${unknownRelationships}. Make sure they've been defined in your model.`; + let unknownRelationshipsMessage = `The payload for '${modelClass.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' }); } }); @@ -2386,27 +2385,27 @@ Store = Service.extend({ }, /** - Build a brand new record for a given type, ID, and + Build a brand new record for a given modelClass, ID, and initial data. @method buildRecord @private - @param {DS.Model} type + @param {DS.Model} modelClass @param {String} id @param {Object} data @return {InternalModel} internal model */ - buildInternalModel(type, id, data) { + buildInternalModel(modelClass, id, data) { heimdall.increment(buildInternalModel); - var typeMap = this.typeMapFor(type); - var idToRecord = typeMap.idToRecord; + let schema = this._schemaForModelClass(modelClass); + let idToRecord = schema.recordMap.idToRecord; - assert(`The id ${id} has already been used with another record of type ${type.toString()}.`, !id || !idToRecord[id]); - assert(`'${Ember.inspect(type)}' does not appear to be an ember-data model`, (typeof type._create === 'function') ); + assert(`The id ${id} has already been used with another record for modelClass ${modelClass.modelName}.`, !id || !idToRecord[id]); + assert(`'${Ember.inspect(modelClass)}' does not appear to be an ember-data model`, (typeof modelClass._create === 'function') ); // lookupFactory should really return an object that creates // instances with the injections applied - var internalModel = new InternalModel(type, id, this, null, data); + var internalModel = new InternalModel(modelClass, id, this, null, data); // if we're creating an item, this process will be done // later, once the object has been persisted. @@ -2414,7 +2413,7 @@ Store = Service.extend({ idToRecord[id] = internalModel; } - typeMap.records.push(internalModel); + schema.recordMap.records.push(internalModel); return internalModel; }, @@ -2437,18 +2436,17 @@ Store = Service.extend({ @param {InternalModel} internalModel */ _dematerializeRecord(internalModel) { - var type = internalModel.type; - var typeMap = this.typeMapFor(type); - var id = internalModel.id; + let recordMap = internalModel.schema.recordMap; + let id = internalModel.id; internalModel.updateRecordArrays(); if (id) { - delete typeMap.idToRecord[id]; + delete recordMap.idToRecord[id]; } - var loc = typeMap.records.indexOf(internalModel); - typeMap.records.splice(loc, 1); + let loc = recordMap.records.indexOf(internalModel); + recordMap.records.splice(loc, 1); }, // ...................... @@ -2456,7 +2454,7 @@ Store = Service.extend({ // ...................... /** - Returns an instance of the adapter for a given type. For + Returns an instance of the adapter for a given modelClass. For example, `adapterFor('person')` will return an instance of `App.PersonAdapter`. @@ -2489,7 +2487,7 @@ Store = Service.extend({ // .............................. /** - Returns an instance of the serializer for a given type. For + Returns an instance of the serializer for a given modelClass. For example, `serializerFor('person')` will return an instance of `App.PersonSerializer`. @@ -2534,16 +2532,16 @@ Store = Service.extend({ @method retrieveManagedInstance @private + @param {String} classType the type of object factory @param {String} modelName the object modelName - @param {String} name the object name @param {Array} fallbacks the fallback objects to lookup if the lookup for modelName or 'application' fails @return {Ember.Object} */ - retrieveManagedInstance(type, modelName, fallbacks) { + retrieveManagedInstance(classType, modelName, fallbacks) { heimdall.increment(retrieveManagedInstance); var normalizedModelName = normalizeModelName(modelName); - var instance = this._instanceCache.get(type, normalizedModelName, fallbacks); + var instance = this._instanceCache.get(classType, normalizedModelName, fallbacks); set(instance, 'store', this); return instance; }, @@ -2607,8 +2605,8 @@ function defaultSerializer(store) { function _commit(adapter, store, operation, snapshot) { var internalModel = snapshot._internalModel; var modelName = snapshot.modelName; - var typeClass = store.modelFor(modelName); - var promise = adapter[operation](store, typeClass, snapshot); + var modelClass = store.modelFor(modelName); + var promise = adapter[operation](store, modelClass, snapshot); var serializer = serializerForAdapter(store, adapter, modelName); var label = `DS: Extract and notify about ${operation} completion of ${internalModel}`; @@ -2622,7 +2620,7 @@ function _commit(adapter, store, operation, snapshot) { store._adapterRun(() => { var payload, data; if (adapterPayload) { - payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, snapshot.id, operation); + payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, snapshot.id, operation); if (payload.included) { store.push({ data: payload.included }); } @@ -2634,7 +2632,7 @@ function _commit(adapter, store, operation, snapshot) { return internalModel; }, function(error) { if (error instanceof InvalidError) { - var errors = serializer.extractErrors(store, typeClass, error, snapshot.id); + var errors = serializer.extractErrors(store, modelClass, error, snapshot.id); store.recordWasInvalid(internalModel, errors); } else { store.recordWasError(internalModel, error); @@ -2649,7 +2647,7 @@ function setupRelationships(store, record, data) { return; } - record.type.eachRelationship((key, descriptor) => { + record.schema.eachRelationship((key, descriptor) => { var kind = descriptor.kind; if (!data.relationships[key]) { diff --git a/tests/unit/store/adapter-interop-test.js b/tests/unit/store/adapter-interop-test.js index d09b19d298b..2882d69560a 100644 --- a/tests/unit/store/adapter-interop-test.js +++ b/tests/unit/store/adapter-interop-test.js @@ -53,10 +53,10 @@ test("Calling Store#find invokes its adapter#find", function(assert) { let done = assert.async(); var adapter = TestAdapter.extend({ - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { assert.ok(true, "Adapter#find was called"); assert.equal(store, currentStore, "Adapter#find was called with the right store"); - assert.equal(type, store.modelFor('test'), "Adapter#find was called with the type passed into Store#find"); + assert.equal(modelClass, store.modelFor('test'), "Adapter#find was called with the modelClass passed into Store#find"); assert.equal(id, 1, "Adapter#find was called with the id passed into Store#find"); assert.equal(snapshot.id, '1', "Adapter#find was called with the record created from Store#find"); @@ -78,10 +78,10 @@ test("Calling Store#findRecord multiple times coalesces the calls into a adapter let done = assert.async(); var adapter = TestAdapter.extend({ - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { assert.ok(false, "Adapter#findRecord was not called"); }, - findMany(store, type, ids, snapshots) { + findMany(store, modelClass, ids, snapshots) { assert.ok(true, "Adapter#findMany was called"); assert.deepEqual(ids, ["1","2"], 'Correct ids were passed in to findMany'); return Ember.RSVP.resolve([{ id: 1 }, { id: 2 }]); @@ -105,7 +105,7 @@ test("Returning a promise from `findRecord` asynchronously loads data", function assert.expect(1); var adapter = TestAdapter.extend({ - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { return resolve({ id: 1, name: "Scumbag Dale" }); } }); @@ -126,7 +126,7 @@ test("IDs provided as numbers are coerced to strings", function(assert) { assert.expect(5); var adapter = TestAdapter.extend({ - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { assert.equal(typeof id, 'string', "id has been normalized to a string"); return resolve({ id: 1, name: "Scumbag Sylvain" }); } @@ -224,8 +224,8 @@ test("loadMany takes an optional Object and passes it on to the Adapter", functi }); var adapter = TestAdapter.extend({ - query(store, type, query) { - assert.equal(type, store.modelFor('person'), 'The type was Person'); + query(store, modelClass, query) { + assert.equal(modelClass, store.modelFor('person'), 'The modelClass was Person'); assert.equal(query, passedQuery, "The query was passed in"); return Ember.RSVP.resolve([]); } @@ -249,7 +249,7 @@ test("Find with query calls the correct normalizeResponse", function(assert) { }); var adapter = TestAdapter.extend({ - query(store, type, query) { + query(store, modelClass, query) { return Ember.RSVP.resolve([]); } }); @@ -277,7 +277,7 @@ test("Find with query calls the correct normalizeResponse", function(assert) { assert.equal(callCount, 1, 'normalizeQueryResponse was called'); }); -test("peekAll(type) returns a record array of all records of a specific type", function(assert) { +test("peekAll(modelClass) returns a record array of all records of a specific modelClass", function(assert) { var Person = DS.Model.extend({ name: DS.attr('string') }); @@ -319,7 +319,7 @@ test("peekAll(type) returns a record array of all records of a specific type", f assert.strictEqual(results, store.peekAll('person'), "subsequent calls to peekAll return the same recordArray)"); }); -test("a new record of a particular type is created via store.createRecord(type)", function(assert) { +test("a new record of a particular modelClass is created via store.createRecord(modelName)", function(assert) { var Person = DS.Model.extend({ name: DS.attr('string') }); @@ -367,10 +367,10 @@ testInDebug("a new record with a specific id can't be created if this id is alre run(function() { store.createRecord('person', { id: 5 }); }); - }, /The id 5 has already been used with another record of type Person/); + }, /The id 5 has already been used with another record for modelClass person/); }); -test("an initial data hash can be provided via store.createRecord(type, hash)", function(assert) { +test("an initial data hash can be provided via store.createRecord(modelName, hash)", function(assert) { var Person = DS.Model.extend({ name: DS.attr('string') }); @@ -416,7 +416,7 @@ test("initial values of attributes can be passed in as the third argument to fin assert.expect(1); var adapter = TestAdapter.extend({ - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { assert.equal(snapshot.attr('name'), 'Test', 'Preloaded attribute set'); return Ember.RSVP.resolve({ id: '1', name: 'Test' }); } @@ -439,7 +439,7 @@ test("initial values of attributes can be passed in as the third argument to fin test("initial values of belongsTo can be passed in as the third argument to find as records", function(assert) { assert.expect(1); var adapter = TestAdapter.extend({ - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { assert.equal(snapshot.belongsTo('friend').attr('name'), 'Tom', 'Preloaded belongsTo set'); return new Ember.RSVP.Promise(function() {}); } @@ -477,7 +477,7 @@ test("initial values of belongsTo can be passed in as the third argument to find assert.expect(1); var adapter = TestAdapter.extend({ - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { return Ember.RSVP.Promise.resolve({ id: id }); } }); @@ -506,7 +506,7 @@ test("initial values of belongsTo can be passed in as the third argument to find test("initial values of hasMany can be passed in as the third argument to find as records", function(assert) { assert.expect(1); var adapter = TestAdapter.extend({ - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { assert.equal(snapshot.hasMany('friends')[0].attr('name'), 'Tom', 'Preloaded hasMany set'); return new Ember.RSVP.Promise(function() {}); } @@ -544,7 +544,7 @@ test("initial values of hasMany can be passed in as the third argument to find a assert.expect(1); var adapter = TestAdapter.extend({ - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { assert.equal(snapshot.hasMany('friends')[0].id, '2', 'Preloaded hasMany set'); return Ember.RSVP.resolve({ id: id }); } @@ -576,7 +576,7 @@ test("records should have their ids updated when the adapter returns the id data var idCounter = 1; var adapter = TestAdapter.extend({ - createRecord(store, type, snapshot) { + createRecord(store, modelClass, snapshot) { return Ember.RSVP.resolve({ name: snapshot.attr('name'), id: idCounter++ }); } }); @@ -635,7 +635,7 @@ test("store.scheduleFetchMany should not resolve until all the records are resol var Phone = DS.Model.extend(); var adapter = TestAdapter.extend({ - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { var wait = 5; var record = { id: id }; @@ -647,7 +647,7 @@ test("store.scheduleFetchMany should not resolve until all the records are resol }); }, - findMany(store, type, ids, snapshots) { + findMany(store, modelClass, ids, snapshots) { var wait = 15; var records = ids.map(function(id) { @@ -699,12 +699,12 @@ test("the store calls adapter.findMany according to groupings returned by adapte ]; }, - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { assert.equal(id, "10", "The first group is passed to find"); return Ember.RSVP.resolve({ id: id }); }, - findMany(store, type, ids, snapshots) { + findMany(store, modelClass, ids, snapshots) { var records = ids.map(function(id) { return { id: id }; }); @@ -750,7 +750,7 @@ test("the promise returned by `scheduleFetch`, when it resolves, does not depend ]; }, - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { var record = { id: id }; return new Ember.RSVP.Promise(function(resolve, reject) { @@ -799,7 +799,7 @@ test("the promise returned by `scheduleFetch`, when it rejects, does not depend ]; }, - findRecord(store, type, id, snapshot) { + findRecord(store, modelClass, id, snapshot) { var record = { id: id }; return new Ember.RSVP.Promise(function(resolve, reject) { @@ -840,7 +840,7 @@ test("store.fetchRecord reject records that were not found, even when those requ var Person = DS.Model.extend(); var adapter = TestAdapter.extend({ - findMany(store, type, ids, snapshots) { + findMany(store, modelClass, ids, snapshots) { var records = ids.map(function(id) { return { id: id }; }); @@ -876,7 +876,7 @@ testInDebug("store.fetchRecord warns when records are missing", function(assert) var Person = DS.Model.extend(); var adapter = TestAdapter.extend({ - findMany(store, type, ids, snapshots) { + findMany(store, modelClass, ids, snapshots) { var records = ids.map(function(id) { return { id: id }; }); @@ -910,7 +910,7 @@ test("store should not call shouldReloadRecord when the record is not in the sto }); var TestAdapter = DS.Adapter.extend({ - shouldReloadRecord(store, type, id, snapshot) { + shouldReloadRecord(store, modelClass, id, snapshot) { assert.ok(false, 'shouldReloadRecord should not be called when the record is not loaded'); return false; }, @@ -938,7 +938,7 @@ test("store should not reload record when shouldReloadRecord returns false", fun }); var TestAdapter = DS.Adapter.extend({ - shouldReloadRecord(store, type, id, snapshot) { + shouldReloadRecord(store, modelClass, id, snapshot) { assert.ok(true, 'shouldReloadRecord should be called when the record is in the store'); return false; }, @@ -972,7 +972,7 @@ test("store should reload record when shouldReloadRecord returns true", function }); var TestAdapter = DS.Adapter.extend({ - shouldReloadRecord(store, type, id, snapshot) { + shouldReloadRecord(store, modelClass, id, snapshot) { assert.ok(true, 'shouldReloadRecord should be called when the record is in the store'); return true; }, @@ -1008,10 +1008,10 @@ test("store should not call shouldBackgroundReloadRecord when the store is alrea }); var TestAdapter = DS.Adapter.extend({ - shouldReloadRecord(store, type, id, snapshot) { + shouldReloadRecord(store, modelClass, id, snapshot) { return true; }, - shouldBackgroundReloadRecord(store, type, id, snapshot) { + shouldBackgroundReloadRecord(store, modelClass, id, snapshot) { assert.ok(false, 'shouldBackgroundReloadRecord is not called when shouldReloadRecord returns true'); }, findRecord() { @@ -1046,7 +1046,7 @@ test("store should not reload a record when `shouldBackgroundReloadRecord` is fa }); var TestAdapter = DS.Adapter.extend({ - shouldBackgroundReloadRecord(store, type, id, snapshot) { + shouldBackgroundReloadRecord(store, modelClass, id, snapshot) { assert.ok(true, 'shouldBackgroundReloadRecord is called when record is loaded form the cache'); return false; }, @@ -1083,7 +1083,7 @@ test("store should reload the record in the background when `shouldBackgroundRel }); var TestAdapter = DS.Adapter.extend({ - shouldBackgroundReloadRecord(store, type, id, snapshot) { + shouldBackgroundReloadRecord(store, modelClass, id, snapshot) { assert.ok(true, 'shouldBackgroundReloadRecord is called when record is loaded form the cache'); return true; }, @@ -1151,7 +1151,7 @@ test("store should reload all records when shouldReloadAll returns true", functi }); var TestAdapter = DS.Adapter.extend({ - shouldReloadAll(store, type, id, snapshot) { + shouldReloadAll(store, modelClass, id, snapshot) { assert.ok(true, 'shouldReloadAll should be called when the record is in the store'); return true; }, @@ -1181,10 +1181,10 @@ test("store should not call shouldBackgroundReloadAll when the store is already }); var TestAdapter = DS.Adapter.extend({ - shouldReloadAll(store, type, id, snapshot) { + shouldReloadAll(store, modelClass, id, snapshot) { return true; }, - shouldBackgroundReloadAll(store, type, id, snapshot) { + shouldBackgroundReloadAll(store, modelClass, id, snapshot) { assert.ok(false, 'shouldBackgroundReloadRecord is not called when shouldReloadRecord returns true'); }, findAll() { @@ -1213,11 +1213,11 @@ test("store should not reload all records when `shouldBackgroundReloadAll` is fa }); var TestAdapter = DS.Adapter.extend({ - shouldReloadAll(store, type, id, snapshot) { + shouldReloadAll(store, modelClass, id, snapshot) { assert.ok(true, 'shouldReloadAll is called when record is loaded form the cache'); return false; }, - shouldBackgroundReloadAll(store, type, id, snapshot) { + shouldBackgroundReloadAll(store, modelClass, id, snapshot) { assert.ok(true, 'shouldBackgroundReloadAll is called when record is loaded form the cache'); return false; },