[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 ...
随机推荐
- diff---比较文件不同
diff命令在最简单的情况下,比较给定的两个文件的不同.如果使用“-”代替“文件”参数,则要比较的内容将来自标准输入.diff命令是以逐行的方式,比较文本文件的异同处.如果该命令指定进行目录的比较,则 ...
- [NOI.AC#40]Erlang
链接 题解 显然,最多抽2个集合 如果一直抽一个,前提是该集合有重复的,答案是不同元素的个数+1 如果抽两个,那么最坏情况下,在一个集合中抽到某一个数的次数是这个集合不同元素的个数(因为抽不到重复的) ...
- php课程 12-40 抽象类的作用是什么
php课程 12-40 抽象类的作用是什么 一.总结 一句话总结:定标准的 1.继承的关键词有哪两个? extendsparent 2.抽象类的实际意义是什么? 制造符合规范的产品你必须实现了抽象类里 ...
- AJAX - 封装AJAX GET 数组join( )方法 键值对取value POST请求参数注意点
function objToStr(obj){ obj.t = new Date().getTime(); // 给obj动态增加了一个属性 // 这个给对象添加属性的方法, 会被直接加到键值对里?? ...
- 洛谷P3403跳楼机(最短路构造/同余最短路)
题目-> 解题思路: 最短路构造很神啊. 先用前两个值跑在第三个值模意义下的同余最短路(这步贪心可以证明,如果第三步长为z,那么如果n+z可以达到,n+2z同样可以达到) 最后计算与楼顶差多少个 ...
- python ATM机 案例代码
利用目前学的流程控制写的 ''' ATM机 需求: 1.登陆 输入账号输入密码 每日只有3次登陆密码错误的机会,超过3次禁止登陆 2.查询余额 3.存款 4.取款 5.转帐 6.退出 ''' info ...
- Spring学习总结(6)——Spring之核心容器bean
一.Bean的基础知识 1.在xml配置文件中,bean的标识(id 和 name) id:指定在benafactory中管理该bean的唯一的标识.name可用来唯一标识bean 或给bean起别名 ...
- 《从零開始学Swift》学习笔记(Day 59)——代码排版
原创文章,欢迎转载.转载请注明:关东升的博客 代码排版包括: 空行.空格.断行和缩进等内容.代码排版内容比較多工作量非常多.可是非常重要. 空行 空行将逻辑相关的代码段分隔开.以提高可读性. 下列情况 ...
- Android: 分页浏览的利器 android View Pager
最近有一个项目需求,水平滑动实现视图切换(分页显示效果) 最先想到的是ImageSwitcher + ViewFilpper 来实现,这效果做出来我自己都不想用,更不用说客户的感觉了:滑动效果生硬,只 ...
- Mycat常见问题与解决方案---宜将剩勇追穷寇,不可沽名学霸王
1 Mycat目前有哪些功能与特性? 答: • 支持 SQL 92标准 • 支持Mysql集群,可以作为Proxy使用 • 支持JDBC连接多数据库 • 支持NoSQL数据库 • 支持galera f ...