【发布时间】:2020-01-16 10:48:21
【问题描述】:
我有一个网格,其中包含要加载的大量数据,因此加载时间变得难以管理。我找到的解决方案是执行连续数量的 http 请求,每个请求检索 100 行的批次,直到完成网格上的所有数据。我知道如何使用 concatMap 实现 2 个连续的 http 请求,它运行良好,但我希望有一个 while 循环来验证每个响应,如果当前行数
使用 concatMap 执行 2 个 http 请求的转发代码:
private LoadGridStringResourcesInProject(projectId: number) {
let allData: StringResource[] = [];
const batchSize = 100;
this.stringResourcesService.getStringResourcesInProject(projectId, 0, batchSize)
.pipe(
concatMap(firstData => {
const stringResourcesInProject = firstData as StringResource[];
// Loads first 100 rows on the Grid
this.gridApi.updateRowData({ add: stringResourcesInProject });
this.agGridService.refreshSizeColumns(this.agGrid);
// Fetch data returned by partial Http requests
allData = stringResourcesInProject;
if (allData && allData.length == batchSize) {
// Do new Http request to fetch the remaining data
return this.stringResourcesService
.getStringResourcesInProject(projectId, batchSize, 0);
}
return [];
})
).subscribe(data => {
const stringResourcesInProject = data as StringResource[];
// Loads the remaining rows in the Grid
this.gridApi.updateRowData({ add: stringResourcesInProject });
// Fetch data returned by partial Http requests
allData = allData.concat(stringResourcesInProject);
},
error => of(null),
() => {
this.agGridService.refreshSizeColumns(this.agGrid);
});
}
【问题讨论】:
-
你要找的接线员是
expand:) -
非常感谢@Maxim1992。像魅力一样工作:)
标签: angular http rxjs concatmap