这是一个开放的 github 问题https://github.com/ngrx/platform/issues/1934
但是,我相信有一种方法可以做到这一点,而无需使用 EntityCacheDispatcher 的saveEntities
方法Save 和 EntityCacheDispatcher.saveEntities()完全从头开始。
为了让您了解按预期使用时它是如何工作的,这是我对最近项目的保存顺序效果
saveOrder$ = createEffect(() =>
this.actions$.pipe(
ofType(PurchaseOrderActions.saveOrder),
switchMap(() => {
const order$ = this.store.pipe(select(selectHeader));
const orderLines$ = this.store.pipe(select(allLines));
const deletedOrderLines$ = this.store.pipe(select(selectDeletedLines));
return combineLatest([order$, orderLines$, deletedOrderLines$]).pipe(
first(),
switchMap(([order, lines, deletedLines]) => {
const changes: ChangeSetItem[] = [
cif.upsert("PurchaseOrder", order),
cif.upsert("PurchaseOrderLine", lines),
cif.delete("PurchaseOrderLine", deletedLines)
];
return this.entityCacheDispatcher
.saveEntities(changes, `${this.baseURL}order`)
.pipe(
map(changesResponse =>
PurchaseOrderActions.saveOrderSuccess({
header: changesResponse.changes[0]
.entities[0] as PurchaseOrder,
lines: changesResponse.changes[1]
.entities as PurchaseOrderLine[]
})
)
);
})
);
})
)
);
潜在路线图
- 创建您的自定义操作,例如类型
"[OrderProductsPage ONINIT] ONINIT"
- 创建调用 saveEntities 的效果
// include in custom action payload and get this id here
const id;
// changeSet with empty changes
const changeSet = {
changes: [],
tag: "CUSTOM_GET_PRODUCTS" // hook for defining custom behaviour
}
this.entityCacheDispatcher.saveEntities(changeSet, `orders/${id}/products`)
- 扩展
EntityCacheDataService
并覆盖该saveEntities
方法以首先检查标记是否CUSTOM_GET_PRODUCTS
调用 super.saveEntities(changeSet, url)。如果标签CUSTOM_GET_PRODUCTS
比调用 get http 和 pipe map 到 changeSetChangeSetOperation.Add
引用super.saveEntities()
更新
这可行,但它比我想要的更hacky