在ngrx中标准化嵌套响应

时间:2019-03-05 20:23:07

标签: angular normalization ngrx

我对ngrx还是陌生的,我不确定关于标准化嵌套对象的最佳实践是什么。

假设商店的定义如下:

export interface User {
   id: string;
   name: string;
   surname: string;
}

export interface Transaction {
    id: string;
    amount: number;
    owners: []
}

export interface TransactionState {
    ids: string[];
    entities: { [id: string]: Transaction }
}

export interface UserState {
   ids: string[],
   entities: { [id: string]: User }
}

export interface CapitalState {
  transactions: TransactionState;
  users: UserState;      
}

我想实现此目标的第一种方法是将数据标准化,然后分派两个操作来更新用户和交易

@Effect()
transactions$ = this.actions$.pipe(
   ofType(fromTransactionsActions.TransactionsActionTypes.LoadTransactions),
   concatMap((action: fromTransactionsActions.LoadTransactions) =>
      this.transactionsService.loadTransactions(action.payload).pipe(
         map((tranactions: TransactionView) => {
             //Normalize Data
             return [ //Dispatch Transactions Load Success
                      //Dispatch Users Load Success];
         }),
         catchError(error => of(new fromTransactionsActions.TransactionsLoadFailure("")))
        ))
    )

第二个是在一个“事务加载成功”操作中调度规范化数据,然后从那里更新状态。

export function reducer(state = initialState, action: TransactionsActions): CapitalState {
    switch (action.type) {
        case TransactionsActionTypes.AllotShareCapitalSuccess:
            return {
                ...state,
                transactions: { ...state.transactions, ...action.payload.entities.transactions },
                users: { ...state.users, ...action.payload.entities.users },
                error: ''
            };

我不确定这些是最佳做法还是缺少什​​么。这些方法中有其中一种吗?有更好的方法吗?

0 个答案:

没有答案