ng-redux icon indicating copy to clipboard operation
ng-redux copied to clipboard

Be able to register multiple reducers with ng-redux

Open jballands opened this issue 9 years ago • 5 comments

I have an application in which I split different pieces of functionality into Angular modules, and then use all the modules I need in a "core module" that actually bootstraps to the page:

let page = angular.module('CoreModule', ['Feature1', 'Feature2', 'Feature3']);
angular.bootstrap(document, ['CoreModule']);

All of these "feature modules" use ngRedux, so their config stages all resemble the following:

module.config(($ngReduxProvider) => {
  $ngReduxProvider.createStoreWith(reducer, [thunk]);
});

The problem is: createStoreWith only allows a single reducer to be registered, meaning that you cannot register reducers from multiple "feature modules" with $ngReduxProvider; in a example with 3 "feature modules", 2 will get smashed and only 1 will survive.

It would be nice if $ngReduxProvider could register multiple reducers, and then the $get method uses combineReducers to combine all the registered reducers together and makes a store out of these combined reducers to create a single injectable instance of a $ngRedux, thus preserving a single store. Usage may look like this:

module.config(($ngReduxProvider) => {
  $ngReduxProvider.registerReducer(reducer, [thunk]);
});

This allows each "feature module" to be responsible for its own reducers. Without this, the "core module" has to import all possible reducers, combine them, and then register them with $ngReduxProvider, breaking the single responsibility principle. Notice how each "feature module" should be able to register middleware with the $ngReduxProvider.

jballands avatar Apr 11 '16 20:04 jballands

Excellent point. I have very little free time to take care of ng-redux this days, so I can't guarantee when I will be able to do that, so if anyone is willing to submit a PR for that, I will gladly accept it! Thanks!

wbuchwalter avatar Apr 11 '16 21:04 wbuchwalter

I was facing the exact same problem, having reducers as constants (the only thing that can be injected during config) was definitely a problem, because it's quite common you want to use a service in reducer (think about an helper function), it also forces us to create a global object (not a constant) for the action types because is the only thing I can get within a constant (the reducers).

If I find some time, would love to make a pull request

Fire-Dragon-DoL avatar Apr 13 '16 17:04 Fire-Dragon-DoL

@Fire-Dragon-DoL, I actually have a modified version of ngRedux with registerReducer working right now that accepts reducers and middleware, but since I never use store enhancers or an initial state, the code is a bit wonky. Would love to collaborate with you sometime and try to improve it for a pull request!

jballands avatar Apr 14 '16 04:04 jballands

Hey, i wrote a new featute for this one #121 for being more precise, stay tuned to see if someone reivews the pull request!

nalejandroveron avatar Jan 11 '17 01:01 nalejandroveron

@Negan1911 functionality is already there and merged

Fire-Dragon-DoL avatar Jan 11 '17 14:01 Fire-Dragon-DoL