基于属性的NgRx实体过滤器状态

时间:2019-08-01 10:42:24

标签: angular ngrx ngrx-entity

我需要在减速器中删除选定的“ archivados”。有什么办法可以过滤当前状态?我寻求以下解决方案,但我认为应该有一种更好的方法。

export const adapter: EntityAdapter<Archivado> = createEntityAdapter<Archivado>(
  {
    sortComparer: (e1: Archivado, e2: Archivado) => e1.id - e2.id
  }
);

export const archivadosInitialState: ArchivadosState = adapter.getInitialState();

export function archivadosReducer(
  state: ArchivadosState = archivadosInitialState,
  action: ArchivadosAction
): any {
  switch (action.type) {
    case ArchivadosActionTypes.DELETE_SELECTED: {
      const ids: number[] = [];
      const entities = { ...state.entities };
      for (const key in Object.keys(entities)) {
        if (entities.hasOwnProperty(key) && entities[key].selected) {
          ids.push(+key);
        }
      }
      return adapter.removeMany(ids, state);
    }
    default: {
      return state;
    }
  }
}

0 个答案:

没有答案