diff --git a/addon/adapters/base.js b/addon/adapters/base.js index 6abace4e..72f744d5 100644 --- a/addon/adapters/base.js +++ b/addon/adapters/base.js @@ -19,9 +19,12 @@ import { singularize, pluralize } from 'ember-inflector'; export default JSONAPIAdapter.extend(ImportExportMixin, { _debug: false, _indices: computed(function() { return {}; }), - isNewSerializerAPI: true, coalesceFindRequests: false, + // TODO: v2.0 - What are the defaults now? What versions to support? + isNewSerializerAPI: true, + + // TODO: v2.0 - Can we deprecate or remove that? What are the defaults now? What versions to support? // Reload behavior shouldReloadRecord() { return true; }, shouldReloadAll() { return true; }, @@ -89,6 +92,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { if (!records) { var url = this.buildURL(type.modelName, null, null, 'queryRecord', query); + // TODO: Document why this is needed or remove it! if (this.sortQueryParams) { query = this.sortQueryParams(query); } @@ -102,6 +106,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { }); }, + // TODO: v2.0 - What are the defaults now? What versions to support? // Delegate to _handleStorageRequest ajax() { return this._handleStorageRequest.apply(this, arguments); @@ -149,8 +154,8 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { _handleGETRequest(url, query) { const { type, id } = this._urlParts(url); - const storage = get(this, '_storage'), - storageKey = this._storageKey(type, id); + const storage = get(this, '_storage'); + const storageKey = this._storageKey(type, id); if (id) { if (!storage[storageKey]) { @@ -208,9 +213,10 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { return null; }, + // TODO: Extract into utility functions in private/query.js _queryFilter(data, serializer, query = {}) { - const queryType = typeOf(query), - dataType = typeOf(data); + const queryType = typeOf(query); + const dataType = typeOf(data); if (queryType === 'object' && dataType === 'object') { return getKeys(query).every((key) => { diff --git a/addon/helpers/storage.js b/addon/helpers/storage.js index b9cd5d38..40161c18 100644 --- a/addon/helpers/storage.js +++ b/addon/helpers/storage.js @@ -2,7 +2,6 @@ import { assert } from '@ember/debug'; import EmberObject, { computed, get } from '@ember/object'; import { getOwner } from '@ember/application'; import { dasherize } from '@ember/string'; -import { deprecate } from '@ember/application/deprecations'; const storage = {}; @@ -32,22 +31,14 @@ function getStorage(name) { let storages = {}; -// TODO: v2.0 - Remove options -function storageFor(key, modelName, options = {}) { - if (arguments.length === 2 && typeof modelName === 'object') { - options = modelName; - modelName = null; - } - - assert('The options argument must be an object', typeof options === 'object'); - - // normalize key +function storageFor(key, modelName) { + // Normalize key key = dasherize(key); if (!modelName) { return computed(function() { if (!storages[key]) { - storages[key] = createStorage(this, key, null, options); + storages[key] = createStorage(this, key, null); } return storages[key]; @@ -56,20 +47,20 @@ function storageFor(key, modelName, options = {}) { assert('The second argument must be a string', typeof modelName === 'string'); + // TODO: Allow callbacks to delete the storage if model gets deleted return computed(modelName, function() { const model = get(this, modelName); - // if the propertyValue is null/undefined we simply return null/undefined + // If the propertyValue is null/undefined we simply return null/undefined if (!model || typeof model === 'undefined') { return model; } const modelKey = _modelKey(model); const storageKey = `${key}:${modelKey}`; - // TODO allow callbacks to delete the storage if model gets deleted if (!storages[storageKey]) { - storages[storageKey] = createStorage(this, key, modelKey, options); + storages[storageKey] = createStorage(this, key, modelKey); } return storages[storageKey]; @@ -80,30 +71,14 @@ function storageFor(key, modelName, options = {}) { * Looks up the storage factory on the container and sets initial state * on the instance if desired. */ -// TODO: v2.0 - Remove options and legacyKey -function createStorage(context, key, modelKey, options) { +function createStorage(context, key, modelKey) { const owner = getOwner(context); const factoryType = 'storage'; const storageFactory = `${factoryType}:${key}`; - - let storageKey; - - if (options.legacyKey) { - deprecate('Using legacyKey has been deprecated and will be removed in version 2.0.0', false, { - id: 'ember-local-storage.storageFor.options.legacyKey', - until: '2.0.0', - url: 'https://github.com/funkensturm/ember-local-storage#deprecations' - }); - - storageKey = options.legacyKey; - } else { - storageKey = modelKey ? `${storageFactory}:${modelKey}` : storageFactory; - } - - storageKey = _buildKey(context, storageKey); + const storageKey = modelKey ? `${storageFactory}:${modelKey}` : storageFactory; const defaultState = { - _storageKey: storageKey + _storageKey: _buildKey(context, storageKey) }; const StorageFactory = owner.factoryFor(storageFactory); @@ -143,7 +118,7 @@ function _modelKey(model) { return `${modelName}:${id}`; } -// TODO: v2.0 - Make modulePrefix the default +// TODO: v2.0 - Make modulePrefix the default - needs a warning/error function _getNamespace(appConfig, addonConfig) { // For backward compatibility this is a opt-in feature let namespace = addonConfig.namespace; diff --git a/tests/unit/legacy-test.js b/tests/unit/legacy-test.js deleted file mode 100644 index 8f7525f1..00000000 --- a/tests/unit/legacy-test.js +++ /dev/null @@ -1,66 +0,0 @@ -import EmberObject from '@ember/object'; -import { module, test } from 'qunit'; -import { setupTest } from 'ember-qunit'; -import { - storageDeepEqual -} from '../helpers/storage'; - -import StorageObject from 'ember-local-storage/local/object'; -import { - storageFor, - _resetStorages -} from 'ember-local-storage/helpers/storage'; - -let subject; - -module('legacy - config', function(hooks) { - setupTest(hooks); - - hooks.beforeEach(function() { - // old serialized content - window.localStorage.settings = JSON.stringify({ - mapStyle: 'dark' - }); - - let mockStorage = StorageObject.extend(); - - mockStorage.reopenClass({ - initialState() { - return { - token: 1234 - }; - } - }); - - this.owner.register('storage:config', mockStorage); - this.owner.register('object:test', EmberObject.extend({ - settings: storageFor('config', { legacyKey: 'settings' }) - })); - subject = this.owner.lookup('object:test'); - }); - - hooks.afterEach(function() { - window.localStorage.clear(); - _resetStorages(); - }); - - test('it has correct defaults', function(assert) { - assert.expect(3); - - assert.equal(subject.get('settings._storageType'), 'local'); - assert.equal(subject.get('settings._storageKey'), 'settings'); - assert.deepEqual(subject.get('settings._initialContent'), { - token: 1234 - }); - }); - - test('serialized content can be used', function(assert) { - assert.expect(2); - - assert.equal(subject.get('settings.mapStyle'), 'dark'); - storageDeepEqual(assert, window.localStorage.settings, { - mapStyle: 'dark', - token: 1234 - }); - }); -});