Skip to content

Commit 2d3a3c9

Browse files
committed
Clarified the connect and select examples in the README (#66)
per feedback from @TobiasKrogh
1 parent 0523060 commit 2d3a3c9

File tree

1 file changed

+62
-26
lines changed

1 file changed

+62
-26
lines changed

README.md

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,43 @@ In this approach, we use `ngRedux.select()` to get observables from slices of ou
7777
state:
7878

7979
```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';
8386

84-
export interface IAppState {
87+
interface IAppState {
8588
counter: number;
86-
// ...
8789
};
8890

8991
@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+
`
91101
})
92-
export class CounterApp {
93-
private count$: Observable<number>;
102+
export class App {
103+
counter$: any;
94104

95105
constructor(private ngRedux: NgRedux<IAppState>) {}
96106

97107
ngOnInit() {
98-
this.count$ = this.ngRedux.select('counter');
108+
let {increment, decrement } = CounterActions;
109+
this.counter$ = this.ngRedux.select('counter');
99110
}
100111

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());
104117
}
105118
```
106119

@@ -113,34 +126,57 @@ Since it's an observable, you can also transform data using observable operators
113126
Alternately you can use the 'ngRedux.connect' API, which will map your state and action creators
114127
to the component class directly.
115128

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.
118132

119133
```typescript
120-
import { Component, AsyncPipe } from 'angular2/core';
134+
import { Component } from 'angular2/core';
135+
import { Counter } from '../components/Counter';
121136
import { NgRedux } from 'ng2-redux';
122-
import { Observable } from 'rxjs';
123-
import * as CounterActions from '../actions/CounterActions';
137+
import { bindActionCreators } from 'redux';
124138

125139
export interface IAppState {
126140
counter: number;
127-
// ...
128141
};
129142

143+
// NB: 'import * as CounterActions' won't provide the right type
144+
// for bindActionCreators.
145+
const CounterActions = require('../actions/CounterActions');
146+
130147
@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+
`
134156
})
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+
}
137173

138-
constructor(ngRedux: NgRedux<IAppState>) {
139-
this.unsubscribe = ngRedux.connect(this.mapStateToTarget, this.mapDispatchToTarget)(this);
174+
mapStateToTarget(state) {
175+
return { counter: state.counter };
140176
}
141177

142178
ngOnDestroy() {
143-
this.unsubscribe();
179+
this.disconnect();
144180
}
145181

146182
// Will result in this.counter being set to the store value of counter

0 commit comments

Comments
 (0)