【发布时间】:2019-06-15 13:36:52
【问题描述】:
我有一种方法可以更新 MySQL 数据库上的“请求”。此请求可以选择包含附件,在这种情况下,我需要发出额外的 HTTP 请求来更新或添加附件记录(到不同的数据库表)并上传文件。
我有一个功能方法来更新请求并可选择跳过附件 HTTP 请求,但想知道实现此目的的更简洁的方法。一般来说,我是 Angular 和 RXJS 的新手,所以 rxjs 运算符的方法和使用可能不是最优的。
基本上,我正在寻找在订阅前有条件地链接一些可选 observable 的最佳方式,或者直接跳到订阅。
我正在寻找使用 iif 的潜在解决方案,返回 Observable.empty() 和不同的 rxjs 运算符,但是当我只想完全跳过它们时,这些似乎是地图函数中的选项。
onUpdateRequest() {
// if there are no attachments added to the request
if (this.attachments.length <= 0) {
this.callUpdateRequest().subscribe(() => {
// some page and form tidy up
});
// there are attachments, so process the new request and then file uploads
} else {
this.callUpdateRequest().pipe(
switchMap(() => {
return of(this.attachments);
}),
mergeMap(attachments => {
return attachments.map(attachment => {
return attachment;
});
}),
mergeMap(attachment => {
return this.attachmentsService.addAttachmentFile(attachment)
.pipe(map(fileData => {
return fileData;
}));
}),
mergeMap(fileData => {
return this.attachmentsService.addAttachment(
this.requestId, fileData.fileUrl
).pipe(
map(attachments => {
return attachments;
})
);
}),
takeLast(1)
)
.subscribe(() => {
// some page and form tidy up
});
)
}
private callUpdateRequest() {
return this.requestsService.updateRequest(
// all the request params
)
}
【问题讨论】:
标签: angular rxjs angular2-observables