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

  1. @ngrx/store only

  2. @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:

  1. Form controller we call the Service to delete the item

  2. Service do the http call to remove the item

  3. Then we have '.do()' method to create side effect, call dispatch

  4. Since the return from Service is Observable, we subscribe form the controller.

  5. 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:

  1. From the controller, we just dispatch the action.

  2. Effect listening the actions that dispatched, and trigger 'ofType' the same is dispatched action from controller.

  3. Inside effect, we call service to delete the widget.

  4. If http request is success, we actually can dispatch another UI action that will do the UI rendering.

  5. If http reuqest is faild, we catch it and you actually can dispatch another UI action that will show the error in UI.

Github

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的更多相关文章

  1. ngrx/store effects 使用总结1:计数器

    本教程案例github:https://github.com/axel10/ngrx_demo-counter-and-list angular2+ 的学习成本应该是三大框架中最高的一个,教程及案例稀 ...

  2. ngrx/store effects 使用总结2:列表展示

    第一个计数器案例:http://www.cnblogs.com/axel10/p/8589122.html 完成了计数器案例后,现在开始比较能够完整的展示angular2+开发流程的案例:在线获取用户 ...

  3. [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 ...

  4. [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 ...

  5. [Angular 2] ngrx/store

    @ngrx/store builds on the concepts made popular by Redux and supercharges it with the backing of RxJ ...

  6. NgRx/Store 4 + Angular 5使用教程

    这篇文章将会示范如何使用NgRx/Store 4和Angular5.@ngrx/store是基于RxJS的状态管理库,其灵感来源于Redux.在NgRx中,状态是由一个包含action和reducer ...

  7. Angular应用架构设计-3:Ngrx Store

    这是有关Angular应用架构设计系列文章中的一篇,在这个系列当中,我会结合这近两年中对Angular.Ionic.甚至Vuejs等框架的使用经验,总结在应用设计和开发过程中遇到的问题.和总结的经验, ...

  8. 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 ...

  9. Course Machine Learning Note

    Machine Learning Note Introduction Introduction What is Machine Learning? Two definitions of Machine ...

随机推荐

  1. AtCoderACGC001C Shorten Diameter

    Description: 给定一个\(n\)个点的树,要求删去最少的点使树的致直径不超过k Solution: 如果\(k\)为偶数,对于最终状态一定是以每一个点为根任何点的深度不能超过\(k/2\) ...

  2. 今日SGU 5.3

    SGU 107 题意:输入一个N,表示N位数字里面有多少个的平方数的结尾9位是987654321 收获:打表,你发现相同位数的数相乘结果的最后几位,就和那两个相乘的数最后几位相乘一样,比如3416*8 ...

  3. wordpress+wampserver

    听说过wordpress和joomla这样的简单建站的工具,尽管是PHP,可是看过同事搭建的公司站点.效果真心不错.于是手痒痒尝试一下.由于是搭着玩儿.所以用wordpress+wampserver( ...

  4. web显示winform,web打开winform,IE打开winform

    前言:为什么要用ie打开winform 个人觉得,winform部署client太麻烦如金蝶··用友,winfrom打补丁太麻烦,加入新功能再部署很费时间:于是就想为什么不能用IE打开呢?这样就不须要 ...

  5. Android获取当前连接的wifi名称

    首先AndroidMainfest.xml文件里加入权限: <uses-permission android:name="android.permission.ACCESS_NETWO ...

  6. Onsctl 配置ONS服务(10G)

    Onsctl Onsctl这个命令是用来管理ONS(Oracle Notification Service)是OracleClustser实现FAN Event Push模型的基础. 在RAC环境下. ...

  7. 14.NPM 常用命令

    转自:http://www.runoob.com/nodejs/nodejs-npm.html PM提供了很多命令,例如install和publish,使用npm help可查看所有命令. NPM提供 ...

  8. Django项目之Web端电商网站的实战开发(一)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶电商项目开发流程 三丶项目需求 四丶项目架构概览 五丶项目数据库设计 六丶项目框架搭建 一丶项目介绍 产品 ...

  9. 基于Lwip协议栈中独立模式下回调函数的使用

    一.使用Lwip协议独立模式开发 最近在STM32F4上边移植了Lwip,Lwip是一个小型开源的TCP/IP协议栈,有无操作系统的支持都可以运行.我当前只测试了TCP Server功能,然后对TCP ...

  10. 关于MyBatis sqlSession的一点整理

    工作中,需要学习一下MyBatis sqlSession的产生过程,翻看了mybatis-spring的源码,阅读了一些mybatis的相关doc,对mybatis sqlSession有了一些认知和 ...