Skip to content

Commit 16d785f

Browse files
committed
demonstrated use of createSelector for composing reducer selectors. need to clean up application state interface, currently selectors are using *any* type to work around type issues
1 parent 45bf657 commit 16d785f

7 files changed

+17
-11
lines changed

app/app.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, ElementRef } from '@angular/core';
22
import { Store, StoreModule } from '@ngrx/store';
33
import { Observable } from 'rxjs/Observable';
44
import { AppStore } from './interface.appstore';
5-
import { getItemValue, getItemValue2, getInteractiveMode } from './reducer.appstate';
5+
import { getItemValue, getItem2Value, getInteractiveMode } from './reducer.appstate';
66

77
@Component({
88
selector: 'my-app',
@@ -45,7 +45,7 @@ export class AppComponent {
4545
//bind the observable appState to the local appState instance
4646
this._appState = this._store.select('appState');
4747
this._itemValue = this._store.select(getItemValue);
48-
this._itemValue2 = this._store.select(getItemValue2);
48+
this._itemValue2 = this._store.select(getItem2Value);
4949

5050
this._interactiveMode = this._store.select(getInteractiveMode);
5151

app/app.module.ts

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { AppComponent } from './app.component';
66
import { Child } from './child.component';
77
import { Child2 } from './child2.component';
88
import { appState } from './reducer.appstate';
9-
import { loanState } from './reducer.loan';
109

1110
@NgModule({
1211
imports: [

app/child.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Input, Component, ngOnChanges } from '@angular/core';
1+
import { Input, Component, OnChanges } from '@angular/core';
22
import { AppStore } from './interface.appstore'
33

44
@Component({

app/child2.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Input, Component, ngOnChanges } from '@angular/core';
1+
import { Input, Component, OnChanges } from '@angular/core';
22
import { AppStore } from './interface.appstore'
33

44
@Component({

app/interface.appstore.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export interface AppStore {
22
id: number;
33
item: string;
4+
item2: {value: string};
5+
interactiveMode: boolean;
46
}

app/reducer.appstate.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// The "appState" reducer performs actions on our application state
22
import {Action} from '@ngrx/store';
33
import {AppStore} from './interface.appstore';
4+
import { createSelector } from 'reselect';
45

56
var defaultState = <AppStore>{id: 0,
67
item: "default",
7-
item2: "default2",
8+
item2: {value: "default2"},
89
interactiveMode: false}
910

1011
export const appState = (state: AppStore = defaultState, action: Action) => {
@@ -14,7 +15,7 @@ export const appState = (state: AppStore = defaultState, action: Action) => {
1415
interactiveMode: state.interactiveMode, item2: state.item2 };
1516
case 'ADD_ITEM2':
1617
return <AppStore>{id: state.id, item: state.item,
17-
interactiveMode: state.interactiveMode, item2: action.payload.item };
18+
interactiveMode: state.interactiveMode, item2: {value: action.payload.item} };
1819
case 'TOGGLE_INTERACTIVE':
1920
return <AppStore>{id: state.id, item: state.item, interactiveMode: !state.interactiveMode, item2: state.item2 };
2021
default:
@@ -23,6 +24,9 @@ export const appState = (state: AppStore = defaultState, action: Action) => {
2324
};
2425

2526

26-
export const getItemValue = (state: AppStore) => state.appState.item;
27-
export const getItemValue2 = (state: AppStore) => state.appState.item2;
28-
export const getInteractiveMode = (state: AppStore) => state.appState.interactiveMode;
27+
export const getItemValue = (state: any) => state.appState.item;
28+
29+
30+
export const getItem2 = (state: any) => state.appState.item2;
31+
export const getItem2Value = createSelector(getItem2, (state: any) => state.value);
32+
export const getInteractiveMode = (state: any) => state.appState.interactiveMode;

systemjs.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
2525
'@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
2626

27-
//@ngrx libraries
27+
//@ngrx related libraries
2828
'@ngrx/core': 'npm:@ngrx/core/bundles/core.umd.js',
2929
'@ngrx/store': 'npm:@ngrx/store/bundles/store.umd.js',
30+
'reselect': 'npm:reselect/dist/reselect.js',
3031

3132
// other libraries
3233
'rxjs': 'npm:rxjs',

0 commit comments

Comments
 (0)