diff --git a/addon/adapters/base.js b/addon/adapters/base.js index 72f744d..e7ae787 100644 --- a/addon/adapters/base.js +++ b/addon/adapters/base.js @@ -2,10 +2,10 @@ import { keys } from '@ember/polyfills'; import RSVP from 'rsvp'; import { run } from '@ember/runloop'; import { isEmpty, typeOf } from '@ember/utils'; -import { computed, get } from '@ember/object'; +import { get } from '@ember/object'; import DS from 'ember-data'; -import ImportExportMixin from '../mixins/adapters/import-export'; -import { _buildKey } from '../helpers/storage'; +import { importData, exportData } from 'ember-local-storage/helpers/import-export'; +import { _buildKey } from 'ember-local-storage/helpers/storage'; const getKeys = Object.keys || keys; @@ -16,24 +16,39 @@ const { // Ember data ships with ember-inflector import { singularize, pluralize } from 'ember-inflector'; -export default JSONAPIAdapter.extend(ImportExportMixin, { - _debug: false, - _indices: computed(function() { return {}; }), - coalesceFindRequests: false, +export default class BaseAdapter extends JSONAPIAdapter { + constructor() { + super(...arguments); + + this._indices = {}; + } + + _debug = false; + coalesceFindRequests = false; // TODO: v2.0 - What are the defaults now? What versions to support? - isNewSerializerAPI: true, + 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; }, - shouldBackgroundReloadRecord() { return true; }, - shouldBackgroundReloadAll() { return true; }, + shouldReloadRecord() { return true; } + shouldReloadAll() { return true; } + shouldBackgroundReloadRecord() { return true; } + shouldBackgroundReloadAll() { return true; } generateIdForRecord() { return Math.random().toString(32).slice(2).substr(0, 8); - }, + } + + // Import + importData(store, content, options) { + return importData(store, content, options); + } + + // Export + exportData(store, types, options) { + return exportData(store, types, options); + } // Relationship sugar createRecord(store, type, snapshot) { @@ -53,8 +68,8 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { } }); - return this._super.apply(this, arguments); - }, + return super.createRecord(...arguments); + } deleteRecord(store, type, snapshot) { snapshot.eachRelationship(function(name, relationship) { @@ -82,12 +97,12 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { } }); - return this._super.apply(this, arguments); - }, + return super.deleteRecord(...arguments); + } // Polyfill queryRecord queryRecord(store, type, query) { - let records = this._super.apply(this, arguments); + let records = super.queryRecord(...arguments); if (!records) { var url = this.buildURL(type.modelName, null, null, 'queryRecord', query); @@ -104,13 +119,13 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { .then(function(result) { return {data: result.data[0] || null}; }); - }, + } // TODO: v2.0 - What are the defaults now? What versions to support? // Delegate to _handleStorageRequest ajax() { return this._handleStorageRequest.apply(this, arguments); - }, + } // Delegate to _handleStorageRequest makeRequest(request) { @@ -119,17 +134,17 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { request.method, { data: request.data } ); - }, + } // Work arround ds-improved-ajax Feature Flag _makeRequest() { return this.makeRequest.apply(this, arguments); - }, + } // Remove the ajax() deprecation warning _hasCustomizedAjax() { return false; - }, + } // Delegate to _handle${type}Request _handleStorageRequest(url, type, options = {}) { @@ -150,7 +165,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { ); } }, 'DS: LocalStorageAdapter#_handleStorageRequest ' + type + ' to ' + url); - }, + } _handleGETRequest(url, query) { const { type, id } = this._urlParts(url); @@ -181,7 +196,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { } return records; - }, + } _handlePOSTRequest(url, record) { const { type, id } = record.data; @@ -191,7 +206,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { get(this, '_storage')[storageKey] = JSON.stringify(record.data); return null; - }, + } _handlePATCHRequest(url, record) { const { type, id } = record.data; @@ -201,7 +216,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { get(this, '_storage')[storageKey] = JSON.stringify(record.data); return null; - }, + } _handleDELETERequest(url) { const { type, id } = this._urlParts(url); @@ -211,7 +226,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { delete get(this, '_storage')[storageKey]; return null; - }, + } // TODO: Extract into utility functions in private/query.js _queryFilter(data, serializer, query = {}) { @@ -286,15 +301,16 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { }); } } - }, + } + // TODO: Extract into utility function _matches(recordValue, queryValue) { if (typeOf(queryValue) === 'regexp') { return queryValue.test(recordValue); } return recordValue === queryValue; - }, + } _urlParts(url) { const parts = url.split('/'); @@ -314,28 +330,28 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { type: type, id: id }; - }, + } _storageKey(type, id) { return _buildKey(this, type + '-' + id); - }, + } // Should be overwriten // Signature: _getIndex(type) _getIndex() { - }, + } _indexHasKey(type, id) { return this._getIndex(type).indexOf(id) !== -1; - }, + } _addToIndex(type, id) { if (!this._indexHasKey(type, id)) { this._getIndex(type).addObject(id); } - }, + } _removeFromIndex(type, id) { this._getIndex(type).removeObject(id); } -}); +} diff --git a/addon/adapters/local.js b/addon/adapters/local.js index 2ec6864..3984f77 100644 --- a/addon/adapters/local.js +++ b/addon/adapters/local.js @@ -3,8 +3,8 @@ import BaseAdapter from './base'; import { getStorage, _buildKey } from '../helpers/storage'; import StorageArray from '../local/array'; -export default BaseAdapter.extend({ - _storage: getStorage('local'), +export default class LocalStorageAdapter extends BaseAdapter { + _storage = getStorage('local'); _getIndex(type) { const indices = get(this, '_indices'); @@ -19,4 +19,4 @@ export default BaseAdapter.extend({ return indices[type]; } -}); +} diff --git a/addon/adapters/session.js b/addon/adapters/session.js index e2c8c90..da24e89 100644 --- a/addon/adapters/session.js +++ b/addon/adapters/session.js @@ -3,8 +3,8 @@ import BaseAdapter from './base'; import { getStorage, _buildKey } from '../helpers/storage'; import StorageArray from '../session/array'; -export default BaseAdapter.extend({ - _storage: getStorage('session'), +export default class SessionStorageAdapter extends BaseAdapter { + _storage = getStorage('session'); _getIndex(type) { const indices = get(this, '_indices'); @@ -19,4 +19,4 @@ export default BaseAdapter.extend({ return indices[type]; } -}); +}