zoukankan      html  css  js  c++  java
  • [Angular 2] Using a Value from the Store in a Reducer

    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
                    };
                })

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

  • 相关阅读:
    node.js 笔记一
    mysql 错误2203 1061 及安装最后出现2003现象的解决办法
    git shell 命令大全
    Mysql常用命令行大全
    php 魔术方法 说明
    php linux 环境搭建
    Linux下源码编译安装MySQL 5.5.8
    linux 压缩解压缩命令
    ftp 命令全集
    sublime text2
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5441173.html
Copyright © 2011-2022 走看看