[Angular2] @Ngrx/store and @Ngrx/effects learning note
Just sharing the learning experience related to @ngrx/store and @ngrx/effects.
In my personal opinion, I fell there are tow different types of coding style by using
@ngrx/store only
- @ngrx/store + @ngrx/effects
So How we do with only ngrx/store?
Controller:
deleteItem(item: Item) {
this.subs['deleteItem'] = this.itemsService.deleteItem(item)
.subscribe(
(res) => {
console.log("Delete Item success", JSON.stringify(res, null, ))
this.resetItem();
},
(err) => alert("error")
);
}
Service:
export class ItemsService {
items$: Observable<Item[]> = this.store.select('items'); constructor(
private http: Http,
private store: Store<AppStore>,
private actions: ItemsActions
) {}
deleteItem(item: Item) {
return this.http.delete(`${BASE_URL}${item.id}`)
.do(action => this.store.dispatch({ type: DELETE_ITEM, payload: item }));
}
Reducer:
case DELETE_ITEM:
return state.filter(item => {
return item[comparator] !== action.payload[comparator];
});
As you can see, the working flow is like that:
Form controller we call the Service to delete the item
Service do the http call to remove the item
Then we have '.do()' method to create side effect, call dispatch
Since the return from Service is Observable, we subscribe form the controller.
Inside successfully handler, we can do any other ui side effect.
ngrx/store + ngrx/effects:
Controller:
deleteWidget(widget: Widget) {
this.store.dispatch({type: DELETE_WIDGET, payload: widget});
}
Service:
deleteWidget(widget: Widget) {
return this.http.delete(`${BASE_URL}${widget.id}`);
}
Effect:
@Effect() delete$: Observable<Action> = this.actions$
.ofType(DELETE_WIDGET)
.map(action => action.payload)
.switchMap((widget)=>{
return this.widgetsService.deleteWidget(widget)
.map(success => console.log("success"))
.catch(() => of("error")) // catch expect to get an observable back
});
Reducer:
case DELETE_WIDGET:
return state.filter(widget => {
return widget[comparator] !== action.payload[comparator];
});
So the work flow for ngrx/store + ngrx/effects:
From the controller, we just dispatch the action.
Effect listening the actions that dispatched, and trigger 'ofType' the same is dispatched action from controller.
Inside effect, we call service to delete the widget.
If http request is success, we actually can dispatch another UI action that will do the UI rendering.
If http reuqest is faild, we catch it and you actually can dispatch another UI action that will show the error in UI.
Summary:
By using only ngrx/store, we are still using the coding style we get used to, like controller talking to the service, after service done its job, return back to controller, let controller do whatever necessary.
By using ngrx/store + ngrx/effects, we are actullay walking into RxJS + Redux world, everything goes reactive, everything should have a reducer even it is UI rendering stuff.
I am not sure which way is better. My point is I am using Redux partten, take advantage of RxJS to help me handle state management esailer. But I don't know to overkill, everything conver to Observable and reducers style is way to far from the starting point -- state management.
[Angular2] @Ngrx/store and @Ngrx/effects learning note的更多相关文章
- ngrx/store effects 使用总结1:计数器
本教程案例github:https://github.com/axel10/ngrx_demo-counter-and-list angular2+ 的学习成本应该是三大框架中最高的一个,教程及案例稀 ...
- ngrx/store effects 使用总结2:列表展示
第一个计数器案例:http://www.cnblogs.com/axel10/p/8589122.html 完成了计数器案例后,现在开始比较能够完整的展示angular2+开发流程的案例:在线获取用户 ...
- [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] Setting up NgRx Router Store and the Time-Travelling Debugger
Make sure you have the@ngrx packages installed: "@ngrx/data": "^8.0.1", "@n ...
- [Angular 2] ngrx/store
@ngrx/store builds on the concepts made popular by Redux and supercharges it with the backing of RxJ ...
- NgRx/Store 4 + Angular 5使用教程
这篇文章将会示范如何使用NgRx/Store 4和Angular5.@ngrx/store是基于RxJS的状态管理库,其灵感来源于Redux.在NgRx中,状态是由一个包含action和reducer ...
- Angular应用架构设计-3:Ngrx Store
这是有关Angular应用架构设计系列文章中的一篇,在这个系列当中,我会结合这近两年中对Angular.Ionic.甚至Vuejs等框架的使用经验,总结在应用设计和开发过程中遇到的问题.和总结的经验, ...
- Learning Note: SQL Server VS Oracle–Database architecture
http://www.sqlpanda.com/2013/07/learning-note-sql-server-vs.html This is my learning note base on t ...
- Course Machine Learning Note
Machine Learning Note Introduction Introduction What is Machine Learning? Two definitions of Machine ...
随机推荐
- windows安装memcached
http://www.cnblogs.com/wujuntian/p/4791220.html
- yum---rpm软件包管理器
yum命令是在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性 ...
- 洛谷——P1137 旅行计划
https://www.luogu.org/problem/show?pid=1137 题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出 ...
- RGB 颜色空间转 HSI 颜色空间的matlab程序实现
RGB 颜色空间转 HSI 颜色空间的matlab程序实现 2014.10.20之前的内容有误,这里依据wikipedia更新了算法内容. 算法以wiki为准 https://en.wikipedia ...
- Exclusive or
题目连接 题意: 每次给一个n.求 (2≤n<10500) 分析: 先说一下自己的想法,假设将n换成二进制数,也就一两千位左右,那么一位一位处理是能够接受的. 将0-n写成二进制形式后,显然全部 ...
- 玩转Bash脚本:选择结构之case
总第5篇 之前,我们谈到了if. 这次我们来谈还有一种选择结构--case. case与if if用于选择的条件,不是非常多的情况,假设选择的条件太多.一系列的if.elif,.也是醉了. 没错,ca ...
- spinner -样式实现
这里主要是在theme中实现spinner的样式,如下 <style name="Theme.Funui" parent="Theme.Holo.Light&qu ...
- windows 静态和动态库
c++中共有两种库:1.动态链接库LIB包含了函数所在的DLL文件和文件中函数位置的信息(入口),代码由运行时加载在进程空间中的DLL提供,称为动态链接库dynamic link library.(这 ...
- js 关于网易淘宝移动端适配的学习
(function (doc, win) { // orientationchange:用户水平或者垂直翻转设备(即方向发生变化)时触发的事件;(屏幕大小发生变化) var docEl = doc.d ...
- UML学习之初步总结
UML(Unified Modeling Language)即统一建模语言,是一种开放的方法,用于说明.可视化.构建和编写一个正在开发的.面向对象的.软件密集系统的制品的开放方法.UML展现了一系列最 ...