Angular - Ngrx实体集合服务库 - 连载

Angular - Ngrx Entity Collection Service Base - Serialization

在我当前的项目中,我有一个带有 nestjs 的后端和一个 angular 中的前端。

我正在使用 ngrx 实体从我的服务后端加载一些数据,如下所示:

日历-event.service.ts

@Injectable({
  providedIn: 'root'
})
export class CalendarEventService extends EntityCollectionServiceBase<CalendarEvent> {

  constructor(elementsFactory: EntityCollectionServiceElementsFactory) {
    super('CalendarEvent', elementsFactory);
  }
}

我有我的实体元数据设置

import {
  EntityMetadataMap,
  EntityDataModuleConfig,
  DefaultDataServiceConfig,
} from '@ngrx/data';

const entityMetadata: EntityMetadataMap = {
  CalendarEvent: {},
};

const pluralNames = {};

export const entityConfig: EntityDataModuleConfig = {
  entityMetadata,
  pluralNames,
};

export const defaultDataServiceConfig: DefaultDataServiceConfig = {
  entityHttpResourceUrls: {
    CalendarEvent: {
      collectionResourceUrl: 'http://localhost:3000/api/calendar/',
      entityResourceUrl: 'http://localhost:3000/api/calendar/',
    },
  },
};

当我从 app.component.ts

加载数据时,我的数据加载正常
import { Observable } from 'rxjs';
import { Component, OnInit } from '@angular/core';
import { CalendarEventService } from './shared/services/calendar-event.service';
import { CalendarEvent } from 'angular-calendar';

@Component({
  selector: 'app-root',
  template: `
    <app-shell class="app-shell">
      <router-outlet></router-outlet>
    </app-shell>
  `,
})
export class AppComponent implements OnInit {
  title = 'memento-front';
  events$: Observable<CalendarEvent[] | any>;

  constructor(private readonly events: CalendarEventService) {
    this.events$ = this.events.filteredEntities$;
  }

  ngOnInit(): void {
    this.events.load();
  }
}

我觉得他们很好:

{ "id": 2, "title": "Second event", "start": "2021-08-13T17:22:43.192Z", "color": { "primary": "#1e90ff", "secondary": "#D1E8FF" } }

我想要实现的是当数据被序列化而不是给出开始字符串日期时,创建一个 Date 对象。这个想法会做类似

的事情
const entityMetadata: EntityMetadataMap = {
  CalendarEvent: {
    filterFn: (events) => {
      events.map(event => event.start = new Date(event.start));
      return events;
    }
  },
};

但不是在调用 filterFn 时执行,而是在调用 http 时执行,在它变得不可变之前执行

我尝试使用 ngrx 实体收集服务库查找序列化,但没有找到任何信息让我知道如何在从 http 调用加载时对数据属性采取一些操作

I tried to look up serialization with ngrx entity collection service base but I didn't find anything letting me know how I could take some action on the data properties upon loading from http call

您可以按照以下步骤在 api 调用时对数据属性执行操作:

app.component.ts:

this.events$ = store.select(selectAllEvents);
this.eventsService.load().subscribe(events => {      
  this.store.dispatch(EventsApiActions.evensLoaded({ events }));
});

app/events/actions/events-api.actions.ts

export const eventsLoaded = createAction(
    "[Events API] Events Loaded Success",
    props<{ events: CalendarEvent[] }>()
);

app/shared/state/events.reducer.ts

export interface State {
  collection: CalendarEvent[];
  ...
}

export const initialState: State = {
    collection: [],
    ...
};

export const eventsReducer = createReducer(
    initialState,
    on(EventsApiActions.eventsLoaded, (state, action) => {
        return {
            ...state,
            collection: action.events
        };
    }),
    ...
);

现在,您可以将 filterFn 的效用函数传递给 filterFn,而不是使用 collection: action.events 改变事件集合:

const mappedEvents = (events: CalendarEvents[]) =>
    events.map((event) => event.start = new Date(event.start))
});

现在你可以在这里使用了:

on(EventsApiActions.eventsLoaded, (state, action) => {
    return {
        ...state,
        collection: mappedEvents(action.events)
    };
}),

最后选择器 (events.reduce.ts):

export const selectAll = (state: State) => state.collection;
export const selectEventsState = (state: State) => state.events;
export const selectAllEvents = createSelector(
    selectEventsState,
    selectAll
);