如何使用@ngrx DB 将单个项目保存在集合中
How do I save individual item in a collection with @ngrx DB
我有以下 @ngrx
效果,它会在搜索完成时触发。 SEARCH_COMPLETE
事件发出 产品集合 。
我想将集合中的每个产品保存到 IndexedDB,而不是将整个集合作为单个值。为实现这一点,我从使用 @ngrx
DB observable 的产品中创建了一个新的 Observable
。我也不得不使用 subscribe
调用来使其工作。
我的问题是,这是正确的做法吗?还是不使用 subscribe
方法更好?
@Effect()
addProductsToDB$: Observable<Action> = this.actions$
.ofType(ProductsSearchActions.ActionTypes.SEARCH_COMPLETE)
.map((action: ProductsSearchActions.SearchCompleteAction) => action.payload)
.switchMap(products => {
return Observable.from(products as IProduct[])
.do(product => {
this.db.insert('products', [{ product }])
.subscribe();
})
.map(() => new ProductDBActions.ProductAddedSuccessAction(true));
});
努力做到
Observable.from(products as IProduct[])
.switchMap(product=> this.db.insert('products', [{ product }]))
.map(() => new ProductDBActions.ProductAddedSuccessAction(true));
我有以下 @ngrx
效果,它会在搜索完成时触发。 SEARCH_COMPLETE
事件发出 产品集合 。
我想将集合中的每个产品保存到 IndexedDB,而不是将整个集合作为单个值。为实现这一点,我从使用 @ngrx
DB observable 的产品中创建了一个新的 Observable
。我也不得不使用 subscribe
调用来使其工作。
我的问题是,这是正确的做法吗?还是不使用 subscribe
方法更好?
@Effect()
addProductsToDB$: Observable<Action> = this.actions$
.ofType(ProductsSearchActions.ActionTypes.SEARCH_COMPLETE)
.map((action: ProductsSearchActions.SearchCompleteAction) => action.payload)
.switchMap(products => {
return Observable.from(products as IProduct[])
.do(product => {
this.db.insert('products', [{ product }])
.subscribe();
})
.map(() => new ProductDBActions.ProductAddedSuccessAction(true));
});
努力做到
Observable.from(products as IProduct[])
.switchMap(product=> this.db.insert('products', [{ product }]))
.map(() => new ProductDBActions.ProductAddedSuccessAction(true));