从NGRX / RXJS中的组件完成第一个操作后,调度第二个操作

时间:2018-06-05 19:29:58

标签: angular rxjs ngrx ngrx-effects rxjs-pipeable-operators

我是新手在我的角度应用程序中使用NGRX / RXJS,我有一种情况,我必须从我的组件调度一个动作,如果该属性为空,则从API获取数据并更新Store_1并设置属性,然后调度另一个操作,该操作使用Store_1中的数据执行Store_2中的某些功能,并在操作1完成后更新状态并填写“pluralconfig”。这是我提出的代码,但我不认为/相信这是最有效的方法,如果我正确使用运算符。

  if(isEmpty(definition.pluralconfig == null))
     {
         this.store$.dispatch(new FormDBAction(definition.formId));

         let id = definition.id;
         this.formLoadSubscription = this.store$.select(getOrderById(id))
         .filter(v => v.pluralconfig != null).subscribe(() =>{

            this.store$.select<TosModel>(getOrderById(id)).subscribe(updatedDefinition => {
              this.store$.dispatch(new OrderDetailAction(updatedDefinition));
            });             
         });
     }
     else{
      this.store$.dispatch(new OrderDetailAction(definition));
     }

1 个答案:

答案 0 :(得分:0)

所有这些逻辑应该是类似

的效果
//Effects file
@Effect()
loadOrderDetail$: = this.actions$.pipe(
ofType(LOAD_ORDER_DETAIL),
flatMap(action => {
    return this.store$.select<TosModel>(getOrderById(action.id)).pipe(first());
})
map(definition => definition.pluralconfig ? new FormDBAction(definition.formId): new OrderDetailAction(definition)})
)

@Effect()
formDb$: = this.actions$.pipe(
ofType(FORM_DB_ACTION),
switchMap(action => {
    ///call api and fire  new FormDbActionSuccess(response.definition) which reducer will store
})

@Effect()
onSuccessOrderDetail$: = this.actions$.pipe(
ofType(FORM_DB_ACTION_SUCESS),
map(action => new OrderDetailAction(action.definition))

//Component
definition$ = this.store.select(getOrderById(id));
store.dispatch( new LoadOrderDetail(id));

https://github.com/ngrx/platform/tree/master/example-app中查看ngrx示例应用 那是我学到的最多