Skip to content

Commit 33efab9

Browse files
committed
createStoreWith accepts either a function or an object with reducers
1 parent 84fc57c commit 33efab9

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/components/ngRedux.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import Connector from './connector';
22
import invariant from 'invariant';
3-
import {createStore, applyMiddleware, compose} from 'redux';
3+
import {createStore, applyMiddleware, compose, combineReducers} from 'redux';
44
import digestMiddleware from './digestMiddleware';
55

66
import assign from 'lodash.assign';
77
import isArray from 'lodash.isarray';
88
import isFunction from 'lodash.isfunction';
9+
import isObejct from 'lodash.isobject';
910

1011
export default function ngReduxProvider() {
1112
let _reducer = undefined;
1213
let _middlewares = undefined;
1314
let _storeEnhancers = undefined;
1415
let _initialState = undefined;
16+
let _reducerIsObejct = undefined;
1517

1618
this.createStoreWith = (reducer, middlewares, storeEnhancers, initialState) => {
1719
invariant(
18-
isFunction(reducer),
19-
'The reducer parameter passed to createStoreWith must be a Function. Instead received %s.',
20+
isFunction(reducer) || isObejct(reducer),
21+
'The reducer parameter passed to createStoreWith must be a Function or an Object. Instead received %s.',
2022
typeof reducer
2123
);
2224

@@ -27,6 +29,7 @@ export default function ngReduxProvider() {
2729
);
2830

2931
_reducer = reducer;
32+
_reducerIsObejct = isObejct(reducer);
3033
_storeEnhancers = storeEnhancers
3134
_middlewares = middlewares || [];
3235
_initialState = initialState;
@@ -43,6 +46,21 @@ export default function ngReduxProvider() {
4346
}
4447
}
4548

49+
if(_reducerIsObejct) {
50+
let reducersObj = {};
51+
let reducKeys = Object.keys(_reducer);
52+
53+
reducKeys.forEach((key) => {
54+
if(typeof _reducer[key] === 'string') {
55+
reducersObj[key] = $injector.get(_reducer[key]);
56+
} else {
57+
reducersObj[key] = _reducer[key];
58+
}
59+
});
60+
61+
_reducer = combineReducers(reducersObj);
62+
}
63+
4664
let finalCreateStore = _storeEnhancers ? compose(..._storeEnhancers)(createStore) : createStore;
4765

4866
//digestMiddleware needs to be the last one.

0 commit comments

Comments
 (0)