首页 > 其他 > 详细

[Angular 2] Using a Value from the Store in a Reducer

时间:2016-04-28 07:01:50      阅读:253      评论:0      收藏:0      [点我收藏+]

RxJS allows you to combine streams in various ways. This lesson shows you how to take a click stream and combine it with a store stream to use a value from the store inside a reducer.

 

The logic is when we click the recall button, it will reset all the people‘s time to the current time.

 

First, bind the click event to recall$:

<button (click)="recall$.next()">Recall</button>

...

recall$ = new Subject();

 

We get the latest time from the time stroe:

    constructor(store:Store) {
        this.time = store.select(clock);
        this.people = store.select(people);


        Observable.merge(
            this.click$,
            this.seconds$,
            this.person$,
            this.recall$
                .withLatestFrom(this.time, (_, y) => y) // _: means don‘t need to care about the first param which is this.recall$
                .map( (time) =>  ({type: RECALL, payload: time}))
            )
            .subscribe(store.dispatch.bind(store))
    }

_: is naming convention, it means, don‘t care about the first value.

 

Last, we handle the action in reducer:

        case RECALL:
            return state.map( (person) => {
                return {
                    name: person.name,
                    time: payload
                };
            })

 

 

-------------

[Angular 2] Using a Value from the Store in a Reducer

原文:http://www.cnblogs.com/Answer1215/p/5441173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!