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. Vue给元素添加样式

    Vue中使用样式 绑定css 数组 <style> .red{ color:red } .thin{ font-size:18px } </style> <h1 :cla ...

  2. 【Codeforces Round #426 (Div. 2) A】The Useless Toy

    [Link]:http://codeforces.com/contest/834/problem/A [Description] [Solution] 开个大小为4的常量字符数组; +n然后余4,-n ...

  3. 洛谷——T1725 探险

    http://codevs.cn/problem/1725/ 时间限制: 1 s  空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解  查看运行结果   题目描述 Descri ...

  4. Asp中JSON的使用

    我对asp全然不懂,因为须要使用json的结构,就研究了一下asp的json的使用,拼接一个json的字符串. 測试用例中使用到了一个lib文件: Json_2.0.3.asp <% ' ' V ...

  5. Windows 98 二十岁了,这些功能都是从它开始的(虽然 Windows 98 不如 Windows 95 那样具有革命性,但完成度更高,更加成熟。到最后还是:相见不如怀念。)

    1998 年 6 月 25 日午夜,美国著名连锁零售店 CompUSA 门外挤满了狂热的消费者和媒体,他们在等待一款软件发售:Windows 98,即使明知它要到当天早上才正式上市. ▲ 在 Comp ...

  6. HTML5手机应用的最大优势就是可以在网页上直接调试和修改

    HTML5手机应用的最大优势就是可以在网页上直接调试和修改

  7. 快速傅里叶变换FFT(模板)

    好不容易闲下来总结一下FFT.QAQ 1.DFT: 对于多项式的乘法,DFT给了我们新的思路(点值表达式的O(n)相乘) 对于我们习惯的多项式算法例如多项式A(x)=5x+1和B(x)=2x+2 C( ...

  8. hdu5301(2015多校2)--Buildings(构造)

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  9. theme-windowAnimationStyle 动画设置

    对于windowAnimationStyle 的引用,目前自己发现的有两处 1.就是直接在Theme 中引用的,如下 <style name="Theme.Funui" pa ...

  10. 关于python的序列和矩阵运算的写法

    #其实下面是这样一个函数,传入的是obj_value,传出的是newobj_value.,, #这里的obj_value实际上是一个序列... for z in obj_value:          ...