Skip to content

Commit

Permalink
Allow customizing your state property key
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 17, 2015
1 parent c7cf466 commit ff9e4dd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
3 changes: 2 additions & 1 deletion dist/alt-browser-with-addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -1503,6 +1503,7 @@ var Alt = (function () {
this.actions = {};
this.stores = {};
this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = "{}";
this._stateKey = config.stateKey;
}

_createClass(Alt, {
Expand Down
3 changes: 2 additions & 1 deletion dist/alt-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -1218,6 +1218,7 @@ var Alt = (function () {
this.actions = {};
this.stores = {};
this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = "{}";
this._stateKey = config.stateKey;
}

_createClass(Alt, {
Expand Down
3 changes: 2 additions & 1 deletion dist/alt-with-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -469,6 +469,7 @@ var Alt = (function () {
this.actions = {};
this.stores = {};
this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = "{}";
this._stateKey = config.stateKey;
}

babelHelpers.createClass(Alt, {
Expand Down
3 changes: 2 additions & 1 deletion dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -487,6 +487,7 @@ var Alt = (function () {
this.actions = {};
this.stores = {};
this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = "{}";
this._stateKey = config.stateKey;
}

_createClass(Alt, {
Expand Down
10 changes: 9 additions & 1 deletion src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand All @@ -460,6 +467,7 @@ class Alt {
this.actions = {}
this.stores = {}
this[LAST_SNAPSHOT] = this[INIT_SNAPSHOT] = '{}'
this._stateKey = config.stateKey
}

dispatch(action, data) {
Expand Down
26 changes: 26 additions & 0 deletions test/custom-state-key-test.js
Original file line number Diff line number Diff line change
@@ -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')
},
}
}

0 comments on commit ff9e4dd

Please sign in to comment.