[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
};
})
-------------
[Angular 2] Using a Value from the Store in a Reducer的更多相关文章
- [Angular 2] Using ngrx/store and Reducers for Angular 2 Application State
ngrx/store is a library that simplifies common RxJS patterns for managing state and gives you an eas ...
- 手把手教你用ngrx管理Angular状态
本文将与你一起探讨如何用不可变数据储存的方式进行Angular应用的状态管理 :ngrx/store——Angular的响应式Redux.本文将会完成一个小型简单的Angular应用,最终代码可以在这 ...
- [Angular 2] Using a Reducer to Change an Object's Property Inside an Array
Reducers are also often used for changing a single property inside of other reducers. This lesson sh ...
- [Angular 2] Passing Observables into Components with Async Pipe
The components inside of your container components can easily accept Observables. You simply define ...
- Redux系列01:从一个简单例子了解action、store、reducer
其实,redux的核心概念就是store.action.reducer,从调用关系来看如下所示 store.dispatch(action) --> reducer(state, action) ...
- 【React】Redux入门 & store体验
组件间传值联动是令人头疼的问题,尤其是一个组件影响多个其他组件状态变化的时候,常常需要一级一级与父组件传值,与父组件的兄弟组件传值等等, 如何化繁为简地处理‘牵一发动全身’的清理就是将所有组件的sta ...
- Redux教程3:添加倒计时
前面的教程里面,我们搭建了一个简单红绿灯示例,通过在console输出当面的倒计时时间:由于界面上不能显示倒计时,用户体验并不良好,本节我们就添加一个简单的倒计时改善一下. 作为本系列的最后一篇文章, ...
- 如何在AngularX 中 使用ngrx
ngrx 是 Angular框架的状态容器,提供可预测化的状态管理. 1.首先创建一个可路由访问的模块 这里命名为:DemopetModule. 包括文件:demopet.html.demopet.s ...
- angular2 学习笔记 ( 状态管理 state management )
更新 : 2017-12-29 ng5 移除 zone.js https://zhuanlan.zhihu.com/p/29577461 zone 的用途就是拦截游览器事件, 比如 click, a ...
随机推荐
- excel - 相等判断
函数使用例子: =IF(EVALUATE(B23)=C23,"正确","错误") 即判断单元格'B23'与'C23'是否相等,若为true,则返回"正 ...
- c#字符串驻留机制
http://www.cnblogs.com/instance/archive/2011/05/24/2056091.html
- Ajax简单应用-购物车
1. 2. 3. 4. 5. 6.
- [转载]C++中声明与定义的区别
C++学了这么多年你知道为什么定义类时,类的定义放在.h文件中,而类的实现放在cpp文件中.它们为什么能够关联到一起呢?你知道什么东西可以放在.h文件中,什么不能.什么东西又可以放在cpp文件中.如果 ...
- C++工厂方法模式
class IFactoryBase { public: IFactoryBase(void); virtual ~IFactoryBase(void); public: virtual IProdu ...
- 10 款强大的JavaScript图表图形插件推荐
转自:http://www.iteye.com/news/24535 网上有很多用于绘制图表图形的免费JavaScript插件和图表库,这类插件大量出现的原因,一是人们不再依赖于Flash,二是浏览器 ...
- groovy构建和解析xml文件
原文链接:http://www.ibm.com/developerworks/cn/java/j-pg05199/ 代码示例: 构建xml文件: def static createXmlFile(){ ...
- 【行为型】Strategy模式
策略模式意图将解决问题的算法分别封装成一个个对象的形式,并使这些算法对象相互间可被替换.模式比较简单,对于策略对象结构的设计,可抽象一个抽象基类,并定义好相关算法(纯)虚接口,并由各种具体的实现算法子 ...
- kafka+storm连接
本项目为maven项目,需要添加必要的storm库,以及kafka依赖,使用storm自带的storm-kafka进行连接,根据自己集群环境 <dependency> <groupI ...
- 关于Chrome(谷歌浏览器)对docume,准确获取网页客户区的宽高、滚动条宽高、滚动条Left和Top
对于document.compatMode,很多朋友可能都根我一样很少接触,知道他的存在却不清楚他的用途.今天在ext中看到 document.compatMode的使用,感觉这个对于我们开发兼容性的 ...