Last active
July 14, 2017 15:22
-
-
Save DaveMBush/b79c333224340b6a9d96ed719b116112 to your computer and use it in GitHub Desktop.
NgRX-With-SubReducers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app/ | |
+-store/ | |
+-route1/ | |
+-sub-reducer1/ | |
--sub-reducer1.actions.ts | |
--sub-reducer1.effects.ts | |
--sub-reducer1.model.ts | |
--sub-reducer1.reducer.ts | |
+-sub-reducer2/ | |
--route1.actions.ts | |
--route1.effects.ts | |
--route1.model.ts | |
--route1.reducer.ts | |
+-route2/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Effect() _save$ = this.update$ | |
.ofType(StagedEditActions.SAVE) | |
.switchMap(() => { | |
return this.store.select(x => x.route1).first(); | |
}) | |
.map(route1Data => this.service.save(route1Data)) | |
.map(requestId$ => { | |
requestId$.subscribe(id => { | |
this.store.dispatch(route1Actions.load(id)); | |
}); | |
return {type: 'noop'}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Route1Model { | |
subItem1?: SubModel1; | |
subItem2?: SubModel2; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function route1Reducer(state: Route1Model = | |
{ | |
subReducer1: {/* subReducer definition here */}, | |
subReducer2: {/* subReducer definition here */} | |
}, action: Action) { | |
// process action here | |
}; | |
export const Route1Reducer: ActionReducer<Route1Model> = route1Reducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducerList = { | |
subReducer1: subReducer1, | |
subReducer2: subReducer2 | |
}; | |
let returnState = state; | |
Object.keys(reducerList).map(item => { | |
const newState = reducerList[item](state[item], action); | |
if(newState !== state[item]) { | |
const newItem = {}; | |
newItem[item] = newState; | |
returnState = Object.assign({}, returnState, newItem); | |
} | |
}); | |
return returnState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
save(event) { | |
event.stopPropagation(); | |
this.store.dispatch(Route1Actions.save()); | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment