From ff9e4dd035b13f93fcb4a4afa372edefaa33d71b Mon Sep 17 00:00:00 2001 From: Josh Perez Date: Fri, 17 Apr 2015 09:11:19 -0700 Subject: [PATCH] Allow customizing your state property key --- dist/alt-browser-with-addons.js | 3 ++- dist/alt-browser.js | 3 ++- dist/alt-with-runtime.js | 3 ++- dist/alt.js | 3 ++- src/alt.js | 10 +++++++++- test/custom-state-key-test.js | 26 ++++++++++++++++++++++++++ 6 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 test/custom-state-key-test.js diff --git a/dist/alt-browser-with-addons.js b/dist/alt-browser-with-addons.js index d44d0326..d001dbc1 100644 --- a/dist/alt-browser-with-addons.js +++ b/dist/alt-browser-with-addons.js @@ -1486,7 +1486,7 @@ function createStoreFromClass(alt, StoreModel, key) { var store = _applyConstructor(Store, argsForConstructor); - storeInstance = assign(new AltStore(alt.dispatcher, store, null, StoreModel), getInternalMethods(StoreModel, builtIns)); + storeInstance = assign(new AltStore(alt.dispatcher, store, typeof alt._stateKey === "string" ? store[alt._stateKey] : null, StoreModel), getInternalMethods(StoreModel, builtIns)); return storeInstance; } @@ -1503,6 +1503,7 @@ var Alt = (function () { this.actions = {}; this.stores = {}; this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = "{}"; + this._stateKey = config.stateKey; } _createClass(Alt, { diff --git a/dist/alt-browser.js b/dist/alt-browser.js index cf9dc34e..3a7f4b3b 100644 --- a/dist/alt-browser.js +++ b/dist/alt-browser.js @@ -1201,7 +1201,7 @@ function createStoreFromClass(alt, StoreModel, key) { var store = _applyConstructor(Store, argsForConstructor); - storeInstance = assign(new AltStore(alt.dispatcher, store, null, StoreModel), getInternalMethods(StoreModel, builtIns)); + storeInstance = assign(new AltStore(alt.dispatcher, store, typeof alt._stateKey === "string" ? store[alt._stateKey] : null, StoreModel), getInternalMethods(StoreModel, builtIns)); return storeInstance; } @@ -1218,6 +1218,7 @@ var Alt = (function () { this.actions = {}; this.stores = {}; this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = "{}"; + this._stateKey = config.stateKey; } _createClass(Alt, { diff --git a/dist/alt-with-runtime.js b/dist/alt-with-runtime.js index 65152258..bf247a19 100644 --- a/dist/alt-with-runtime.js +++ b/dist/alt-with-runtime.js @@ -453,7 +453,7 @@ function createStoreFromClass(alt, StoreModel, key) { var store = babelHelpers.applyConstructor(Store, argsForConstructor); - storeInstance = assign(new AltStore(alt.dispatcher, store, null, StoreModel), getInternalMethods(StoreModel, builtIns)); + storeInstance = assign(new AltStore(alt.dispatcher, store, typeof alt._stateKey === "string" ? store[alt._stateKey] : null, StoreModel), getInternalMethods(StoreModel, builtIns)); return storeInstance; } @@ -469,6 +469,7 @@ var Alt = (function () { this.actions = {}; this.stores = {}; this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = "{}"; + this._stateKey = config.stateKey; } babelHelpers.createClass(Alt, { diff --git a/dist/alt.js b/dist/alt.js index cae84289..910dcf90 100644 --- a/dist/alt.js +++ b/dist/alt.js @@ -470,7 +470,7 @@ function createStoreFromClass(alt, StoreModel, key) { var store = _applyConstructor(Store, argsForConstructor); - storeInstance = assign(new AltStore(alt.dispatcher, store, null, StoreModel), getInternalMethods(StoreModel, builtIns)); + storeInstance = assign(new AltStore(alt.dispatcher, store, typeof alt._stateKey === "string" ? store[alt._stateKey] : null, StoreModel), getInternalMethods(StoreModel, builtIns)); return storeInstance; } @@ -487,6 +487,7 @@ var Alt = (function () { this.actions = {}; this.stores = {}; this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = "{}"; + this._stateKey = config.stateKey; } _createClass(Alt, { diff --git a/src/alt.js b/src/alt.js index 36dbdcc6..25dd9b0d 100644 --- a/src/alt.js +++ b/src/alt.js @@ -445,7 +445,14 @@ function createStoreFromClass(alt, StoreModel, key, ...argsForConstructor) { const store = new Store(...argsForConstructor) storeInstance = assign( - new AltStore(alt.dispatcher, store, null, StoreModel), + new AltStore( + alt.dispatcher, + store, + typeof alt._stateKey === 'string' + ? store[alt._stateKey] + : null, + StoreModel + ), getInternalMethods(StoreModel, builtIns) ) @@ -460,6 +467,7 @@ class Alt { this.actions = {} this.stores = {} this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = '{}' + this._stateKey = config.stateKey } dispatch(action, data) { diff --git a/test/custom-state-key-test.js b/test/custom-state-key-test.js new file mode 100644 index 00000000..541c25d7 --- /dev/null +++ b/test/custom-state-key-test.js @@ -0,0 +1,26 @@ +import { assert } from 'chai' +import Alt from '../dist/alt-with-runtime' + +const alt = new Alt({ + stateKey: 'state' +}) + +class Store { + static displayName: 'CustomStateKeyStore' + + constructor() { + this.foo = 1 + this.state = { yes: 2 } + } +} + +const store = alt.createStore(Store) + +export default { + 'Custom State Key': { + 'setting a custom key for declaring state'() { + assert.isUndefined(store.getState().foo, 'foo is private') + assert.isDefined(store.getState().yes, 'yes is part of state') + }, + } +}