@@ -77,30 +77,43 @@ In this approach, we use `ngRedux.select()` to get observables from slices of ou
77
77
state:
78
78
79
79
``` typescript
80
- import { NgRedux } from ' ng2-redux' ;
81
- import { Observable } from ' rxjs' ;
82
- import { increment , decrement } from ' ../actions/CounterActions' ;
80
+ import { Component } from ' angular2/core' ;
81
+ import { Observable } from ' rxjs' ;
82
+ import { AsyncPipe } from ' angular2/common' ;
83
+ import { Counter } from ' ../components/Counter' ;
84
+ import * as CounterActions from ' ../actions/CounterActions' ;
85
+ import { NgRedux } from ' ng2-redux' ;
83
86
84
- export interface IAppState {
87
+ interface IAppState {
85
88
counter: number ;
86
- // ...
87
89
};
88
90
89
91
@Component ({
90
- // ...
92
+ selector: ' root' ,
93
+ directives: [Counter ],
94
+ pipes: [AsyncPipe ],
95
+ template: `
96
+ <counter [counter]="counter$| async"
97
+ [increment]="increment"
98
+ [decrement]="decrement">
99
+ </counter>
100
+ `
91
101
})
92
- export class CounterApp {
93
- private count $: Observable < number > ;
102
+ export class App {
103
+ counter $: any ;
94
104
95
105
constructor (private ngRedux : NgRedux <IAppState >) {}
96
106
97
107
ngOnInit() {
98
- this .count$ = this .ngRedux .select (' counter' );
108
+ let {increment, decrement } = CounterActions ;
109
+ this .counter$ = this .ngRedux .select (' counter' );
99
110
}
100
111
101
- increment() {
102
- this .ngRedux .dispatch (increment ());
103
- }
112
+ incrementIfOdd = () => this .ngRedux .dispatch (
113
+ <any >CounterActions .incrementIfOdd ());
114
+
115
+ incrementAsync = () => this .ngRedux .dispatch (
116
+ <any >CounterActions .incrementAsync ());
104
117
}
105
118
```
106
119
@@ -113,34 +126,57 @@ Since it's an observable, you can also transform data using observable operators
113
126
Alternately you can use the 'ngRedux.connect' API, which will map your state and action creators
114
127
to the component class directly.
115
128
116
- This pattern is handy if your component has a large number of distinct actions and
117
- properties to feed to its render tree of presentational components:
129
+ This pattern is provided for backwards compatibility. It's worth noting that
130
+ Angular 2's view layer is more optimized for Observables and the ` select `
131
+ pattern above.
118
132
119
133
``` typescript
120
- import { Component , AsyncPipe } from ' angular2/core' ;
134
+ import { Component } from ' angular2/core' ;
135
+ import { Counter } from ' ../components/Counter' ;
121
136
import { NgRedux } from ' ng2-redux' ;
122
- import { Observable } from ' rxjs' ;
123
- import * as CounterActions from ' ../actions/CounterActions' ;
137
+ import { bindActionCreators } from ' redux' ;
124
138
125
139
export interface IAppState {
126
140
counter: number ;
127
- // ...
128
141
};
129
142
143
+ // NB: 'import * as CounterActions' won't provide the right type
144
+ // for bindActionCreators.
145
+ const CounterActions = require (' ../actions/CounterActions' );
146
+
130
147
@Component ({
131
- pipes: [ AsyncPipe ]
132
- template : ' <p>Count: {{ count$ | async }}</p>'
133
- // ...
148
+ selector: ' root' ,
149
+ directives: [Counter ],
150
+ template: `
151
+ <counter [counter]="counter"
152
+ [increment]="actions.increment"
153
+ [decrement]="actions.decrement">
154
+ </counter>
155
+ `
134
156
})
135
- export class CounterApp {
136
- private count$: Observable <number >;
157
+ export class App {
158
+ private disconnect: (a ? : any ) => void ;
159
+
160
+ constructor (private ngRedux : NgRedux <IAppState >) {}
161
+
162
+ ngOnInit() {
163
+ this .disconnect = this .ngRedux .connect (
164
+ this .mapStateToTarget ,
165
+ this .mapDispatchToTarget )(this );
166
+ }
167
+
168
+ mapDispatchToTarget(dispatch ) {
169
+ return {
170
+ actions: bindActionCreators (CounterActions , dispatch )
171
+ };
172
+ }
137
173
138
- constructor ( ngRedux : NgRedux < IAppState > ) {
139
- this . unsubscribe = ngRedux . connect ( this . mapStateToTarget , this . mapDispatchToTarget )( this ) ;
174
+ mapStateToTarget( state ) {
175
+ return { counter: state . counter } ;
140
176
}
141
177
142
178
ngOnDestroy() {
143
- this .unsubscribe ();
179
+ this .disconnect ();
144
180
}
145
181
146
182
// Will result in this.counter being set to the store value of counter
0 commit comments