Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be able to register multiple reducers with ng-redux #75

Open
jballands opened this issue Apr 11, 2016 · 5 comments
Open

Be able to register multiple reducers with ng-redux #75

jballands opened this issue Apr 11, 2016 · 5 comments

Comments

@jballands
Copy link

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.

@wbuchwalter
Copy link
Member

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!

@Fire-Dragon-DoL
Copy link

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

@jballands
Copy link
Author

@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!

@Negan1911
Copy link

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

@Fire-Dragon-DoL
Copy link

@Negan1911 functionality is already there and merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants