Skip to content

Instantly share code, notes, and snippets.

@pauleveritt
Created April 19, 2016 18:02
Show Gist options
  • Save pauleveritt/a86ccbe510bb754107354274dfae9052 to your computer and use it in GitHub Desktop.
Save pauleveritt/a86ccbe510bb754107354274dfae9052 to your computer and use it in GitHub Desktop.
Async pipe on object
import {Component} from "angular2/core";
import {Store, Reducer} from "@ngrx/store";
import {Observable} from "rxjs/Observable";
interface AppState {
counter: number;
}
interface ICounter {
id: number,
value: number
}
const INCREMENT = 'INCREMENT';
const counter: Reducer<ICounter> = (state: ICounter = { id: 1, value: 0 },
{ type, payload }) => {
switch (type) {
case INCREMENT:
return { id: payload.id, value: payload.value++ };
default:
return state;
}
};
@Component({
selector: 'my-app',
template: `
<span>Counter: {{ counter.value | async }}</span>
<button (click)="increment()">Increment</button>
`
})
export class AppComponent {
counter: Observable<ICounter>;
constructor(public store: Store<AppState>) {
this.counter = store.select('counter');
}
increment() {
const payload: ICounter = { id: 1, value: 0 };
this.store.dispatch({ type: INCREMENT, payload });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment