I am working on a Vue application that includes several form fields. I want to ensure that any changes made by the user are saved in real-time to a backend database using a REST API with Axios, without requiring the user to click a save button.
I have two queries regarding this:
#1: Which event should I monitor to trigger my API calls?
Is it appropriate to use a v-on:change binding, or would this event be triggered too frequently (with every keystroke)?
<input type="text" v-model="userName" v-on:change="signalChange">
methods:{
signalChange: function(evt){
axios.put(this.getRootURL + 'app/save.php', {
recordId: this.$route.params.recordid,
userName: this.userName
}).then(response => {
console.log("Change saved...")
}).catch(e => {
console.log("Error... ")
}
}
#2: Is it necessary to queue changes to ensure that the last change is saved properly?
For example, if a user quickly toggles a button back and forth, there might be a delay due to the asynchronous nature of Axios, causing the last change to not be saved last on the server. This could lead to a discrepancy between the UI and backend data.
Would it be advisable to create a Sync utility that stores changes in a queue array and waits for each change to complete before sending the next one to the server? Are there any existing libraries or code samples available for this purpose?
Thank you!