Skip to content

Commit 912f5d1

Browse files
committed
Merge pull request #76 from Carburetor/reducer-as-function-and-object
createStoreWith accepts either a function or an object with reducers
2 parents 84fc57c + 4099327 commit 912f5d1

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

Diff for: README.md

+25
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Add the following script tag to your html:
4141

4242
#### Initialization
4343

44+
You can initialize ngRedux two ways:
45+
46+
Function as the reducer argument for the `createStoreWith`:
47+
4448
```JS
4549
import reducers from './reducers';
4650
import { combineReducers } from 'redux';
@@ -54,6 +58,27 @@ angular.module('app', [ngRedux])
5458
});
5559
```
5660

61+
A reducer object as reducer argument for the `createStoreWith`:
62+
63+
```JS
64+
import reducers from './reducers';
65+
import { combineReducers } from 'redux';
66+
import loggingMiddleware from './loggingMiddleware';
67+
import ngRedux from 'ng-redux';
68+
69+
angular.module('app', [ngRedux])
70+
.config(($ngReduxProvider) => {
71+
reducer3 = functtion(state, action){}
72+
$ngReduxProvider.createStoreWith({
73+
reducer1: "reducer1",
74+
reducer2: function(state, action){},
75+
reducer3: reducer3
76+
}, ['promiseMiddleware', loggingMiddleware]);
77+
});
78+
```
79+
80+
The object can be constructed either by passing a function as value or a string representing the reducer. This way, you can create reducers as services and initialze them inside the `.config`. Behind the secnes, ngRedux will `$injector.get` the string you pass as the value for the ojects of reducers and initilaze it.
81+
5782
#### Usage
5883

5984
*Using controllerAs syntax*

Diff for: src/components/ngRedux.js

+21-3
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 isObject 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 _reducerIsObject = 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) || isObject(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+
_reducerIsObject = isObject(reducer);
3033
_storeEnhancers = storeEnhancers
3134
_middlewares = middlewares || [];
3235
_initialState = initialState;
@@ -43,6 +46,21 @@ export default function ngReduxProvider() {
4346
}
4447
}
4548

49+
if(_reducerIsObject) {
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)