diff --git a/README.md b/README.md index e1abeea..0c5b710 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ export default Ember.Component.extend({ #### Namespace & keyDelimiter -In you apps `config/environment.js` you can set a `namespace` and a `keyDelimiter`. For backward compatibility this is a opt-in feature. +In your `config/environment.js` you can set a `namespace` and a `keyDelimiter` option. For backward compatibility this is a opt-in feature. **Important:** Don't turn this feature on for existing apps. You will lose access to existing keys. @@ -81,6 +81,24 @@ module.exports = function() { }; ``` +#### Window Sync + +In your `config/environment.js` you can set a `sync` option. For backward compatibility this is a opt-in feature and defaults to true. + +If set to `false`, this will disable immediate syncing across tabs/windows. + +```js +// config/environment.js +module.exports = function() { + var ENV = { + modulePrefix: 'my-app', + 'ember-local-storage': { + sync: true // disable syncing across windows + } + } +}; +``` + #### ember-data support This addon autodetects if you use ember-data and will include the support for ember-data adapters and serializes by default. You can opt out of this behavior by setting the `includeEmberDataSupport` option to `false`: diff --git a/addon/helpers/storage.js b/addon/helpers/storage.js index b5137d3..6a55b57 100644 --- a/addon/helpers/storage.js +++ b/addon/helpers/storage.js @@ -77,9 +77,12 @@ function createStorage(context, key, modelKey) { const storageKey = modelKey ? `${storageFactory}:${modelKey}` : storageFactory; + let appConfig = owner.resolveRegistration('config:environment'); + let addonConfig = (appConfig && appConfig['ember-local-storage']) || {}; const defaultState = { _storageKey: _buildKey(context, storageKey), + _sync: _getSync(appConfig, addonConfig), }; const StorageFactory = owner.factoryFor(storageFactory); @@ -130,6 +133,17 @@ function _getNamespace(appConfig, addonConfig) { return namespace; } +function _getSync(appConfig, addonConfig) { + // For backward compatibility this is a opt-in feature + let sync = addonConfig.sync; + + if (sync === undefined) { + sync = appConfig.sync; + } + + return sync; +} + // TODO: Add migration helper function _buildKey(context, key) { let appConfig = getOwner(context).resolveRegistration('config:environment'); diff --git a/addon/mixins/storage.js b/addon/mixins/storage.js index ae82651..0765602 100644 --- a/addon/mixins/storage.js +++ b/addon/mixins/storage.js @@ -21,6 +21,7 @@ export default Mixin.create({ const storage = this._storage(); const storageKey = get(this, '_storageKey'); const initialContent = get(this, '_initialContent'); + const sync = get(this, '_sync'); let serialized, content; @@ -39,7 +40,9 @@ export default Mixin.create({ this.set('content', content); // Keep in sync with other windows - this._addStorageListener(); + if (sync !== false) { + this._addStorageListener(); + } return this._super(...arguments); },