[英]ngrx effects for a service loop
I'm new to ngrx and trying to get the @Effect function for my ngrx store working. 我是ngrx的新手,正在尝试为我的ngrx存储工作使用@Effect函数。 The code below shows how the services work if I wasn't using a ngrx store.
下面的代码显示了如果我不使用ngrx存储,服务将如何工作。 I first make an http.get call to get a list, and then iterate on this list to get each list-item's.
我首先进行http.get调用以获取列表,然后在该列表上进行迭代以获取每个列表项。
export interface State {
listItems: ItemDescription[];
itemsData: ItemData[];
}
this.http.get('list-url').subscribe(listItems => {
listItems.map(item => {
this.http.get('list-item-url/' + item.id).subscribe(itemdata => {
state.itemsData.push(itemdata);
});
});
});
How is my @Effect function supposed to look like? 我的@Effect函数应该看起来如何? Do I need to break this up into two @Effect functions, one to grab the list, another to get the items?
我是否需要将其分解为两个@Effect函数,一个用于获取列表,另一个用于获取项目?
I would, 我会,
I suggest, you should check and learn best practices from example application . 我建议您应该检查示例应用程序并从中学习最佳实践。
For reference, @okan-aslankan, I solved the problem with the following code based on: 作为参考,@ okan-aslankan,我基于以下代码解决了此问题:
Handle multiple http requests and wait for all to complete using Angular RxJs Observables 使用Angular RxJs Observables处理多个http请求并等待所有请求完成
and split into two @Effect functions: 并分成两个@Effect函数:
@Effect()
listGet$: Observable<Action> = this.actions$
.ofType(items.LIST_UPDATE)
.switchMap(() => this.StoreServ.list(AppSettings.LIST_DIR)
.switchMap(listOfItems => [new ListUpdatedAction(listOfItems), new ReadUpdateAction(listOfItems)]));
@Effect()
readData$: Observable<Action> = this.actions$
.ofType(items.READ_UPDATE)
.mergeMap(listData => this.StoreServ.readData(listData)
.switchMap(items => [new ReadUpdatedAction(items)])
);
where: 哪里:
public list (name: string): Observable<ListResponse> {
return fromPromise(this.itemStore.list(name));
}
public read (logId: string): Observable<PlainLogTable> {
return fromPromise(this.itemStore.read(logId));
}
public readData(list): Observable<PlainLogTable[]> {
return forkJoin(list.payload.map(item => this.read(item.id)));
}
I don't know, I've heard good things about RxJS, but can't help but notice the steep learning curve and non-obvious (convoluted?) solution to an async problem which seems quite obvious w/ the initial http.get code. 我不知道,我已经听说过有关RxJS的好消息,但是不禁注意到陡峭的学习曲线和对异步问题的非显而易见(令人费解的?)解决方案,这与初始的http.get相当明显。码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.