[英]Implement Autosave to Firestore database when using Angular Reactive Forms
I have an Angular app where the data in a form needs to be saved to (and updated from) a Firestore database more or less instantly since 1) multiple people will be entering data on the same form, 2) other users are going to be using the real time values in another part of the application, and 3) the data needs to be saved in case of power or internet failure.我有一个 Angular 应用程序,其中表单中的数据需要或多或少地立即保存到(并从)Firestore 数据库更新,因为 1)多人将在同一个表单上输入数据,2)其他用户将在应用程序的另一部分使用实时值,以及 3) 数据需要在电源或互联网故障的情况下保存。
I'm using reactive forms and I've tried saving the form values by subscribing to the form changes and also by triggering individual changes on keyup.我正在使用响应式表单,并且我尝试通过订阅表单更改以及触发 keyup 上的单个更改来保存表单值。 For the most part, it works ok, but sometimes the data gets lost.大多数情况下,它工作正常,但有时数据会丢失。
For example, let's say I type something into a field (eg 123456789).例如,假设我在字段中输入了一些内容(例如 123456789)。 Here is what sometimes happens:以下是有时会发生的情况:
Is there a good way to handle this?有没有好的方法来处理这个问题? I've tried to use debounceTime, but that sort of exacerbates the delay problem.我曾尝试使用 debounceTime,但这会加剧延迟问题。
I've got a sample stackblitz where I simulate the firestore database delay issue ( https://stackblitz.com/edit/immediate-save?file=src/app/app.component.ts ).我有一个示例 stackblitz,我在其中模拟了 Firestore 数据库延迟问题( https://stackblitz.com/edit/immediate-save?file=src/app/app.component.ts )。
ps I'd be OK with holding off on a save until the focus changes (eg blur or (change)), but I would also want to trigger a save if a cursor stops typing in a field, but doesn't tab to the next field ps 我可以在焦点改变(例如模糊或(改变))之前推迟保存,但如果光标停止在字段中输入,但我也想触发保存,但没有标签到下一个字段
How does adding debounce time exacerbates delay problem?添加去抖时间如何加剧延迟问题? Just add debounce on valueChanges lisener, not database request:只需在 valueChanges lisener 上添加 debounce,而不是数据库请求:
this.controls.valueChanges
.pipe(
debounceTime(500)
)
.subscribe(person=>{
console.log(person);
this.dataSvc.updatePerson(person);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.