1

我的@ngrx/data 用例要求我通过扩展来构建自定义实体数据服务DefaultDataService<T>。我正在使用的 API 接受 JSON 补丁文档进行更新。EntityCollectionDataService<T>定义以下更新函数:

update(update: Update<T>): Observable<T>;

哪里有类型Update<T>的成员。我在这里面临的问题是,我无法仅使用实体更改字段的一部分来形成 JSON 补丁文档。我需要实体的未更改和更改状态。changesPartial<T>

我在这里可以看到的唯一解决方案是EntityChangeTracker通过EntityCollectionService我的实体访问,我只是对如何使用更改跟踪器有点困惑,因为它只是通过 EntityCollectionServicechangeState$字段的可观察的更改流。

有没有更简单的方法我在这里看不到?我还想只访问商店并拉取实体的当前状态,但我更喜欢使用乐观并发,所以在DefaultDataService我写的时候通过更新的副作用调用商店已经改变了。

4

1 回答 1

0

事实证明,您可以按照我的预期通过观察 EntityChangeTracker 中发生的更改来形成补丁文档。只需订阅changeState$,检查状态更改是否为更新,然后继续比较原始内容与修改后的内容。

为此,我在update方法的数据服务中返回一个 Observable,并检查更新的内容是否在changeState地图中有键。

update = (entity: Update<TEntity>): Observable<TEntity> => {
    /**
     * Since we are needing to perform extra logic to
     * retrieve an original copy of the modified entity,
     * we are returning a new Observable.
     */
    return new Observable<TEntity>((subscriber) => {
      // Pull the entity change state for this updated model
      // from the entity change tracker.
      const entityChangeState: ChangeState<TEntity> | undefined = this.changeStateMap[entity.id];

     // Here, check the entityChangeState and compare it with the original record 

...
于 2021-07-22T16:11:47.600 回答