I have a Vue component with an input with which I used v-model
. Theorically it works fine but in practice, it works really bad, if I type too quickly, the input value is broken (for example, typing azertyuiop
will result with auip
).
I can't type slowly as the input will be filled by scanning barcodes (real quick).
Each time the user types, it makes a request using axios, and I guess that's the source of the issue but I have no idea what I can do about it.
How could I avoid this issue ?
Here is my Ordeline.vue:
<template>
<div class="field">
<label class="label">{{ fullname }}</label>
<div class="control">
<input
:class="'input' + (value.valid ? '' : ' is-danger')"
v-model="modelValue"
type="text">
</div>
<p v-if="error !== null" class="help is-danger">
<span class="tag is-danger">ERREUR</span>
{{ error }}
</p>
</div>
</template>
<script>
import api from '../axios-wrapper';
export default {
name: "orderline",
props: {
value: {required: true},
apiTarget: {required: true}
},
computed: {
modelValue: {
get() {
return this.value.imei;
},
set(val) {
this.validate(val);
}
},
fullname () {
return this.value.title + ' ' + this.value.grade_name;
// return this.value.make_name + ' ' + this.value.color_name + ' ' + this.value.expected_memory + 'Go ' + this.value.grade_name;
}
},
data () {
return {
error: null
}
},
methods: {
validate (val) {
api
.post(this.apiTarget, {
imei: val,
make_id: this.value.expected_make,
make_name: this.value.make_name,
color_id: this.value.expected_color,
color_name: this.value.color_name,
memory: this.value.expected_memory,
grade_id: this.value.expected_grade,
grade_name: this.value.grade_name,
})
.then(response => {
this.error = null;
let clone = this.value;
clone.imei = val;
clone.valid = true;
this.$emit('input', clone);
})
.catch(error => {
this.error = error.response.data.message;
let clone = this.value;
clone.imei = val;
clone.valid = false;
this.$emit('input', clone);
})
;
}
},
mounted() {
this.validate(this.modelValue);
}
}
</script>