I am working with data retrieved from a MySQL database where "1" and "0" represent boolean true and false. In my Vue component, I have set these values as shown below:
data(){
return {
form : {
attribute_1 : "1", //attribute 1 is true
attribute_2 : "0", //attribute 2 is false
attribute_3 : "1", //attribute 3 is true
}
}
}
To maintain two-way binding, I have implemented computed properties like this:
attribute1: {
get(){
return this.form.attribute_1 == "1" ? true : false ;
},
set(newValue){
this.form.attribute_1 = newValue ? "1" : "0";
}
},
attribute2: {
get(){
return this.form.attribute_2 == "1" ? true : false ;
},
set(newValue){
this.form.attribute_2 = newValue ? "1" : "0";
}
}, ...
These computed properties are used in the HTML code like so:
<input type="checkbox" checked v-model="attribute1">
<input type="checkbox" checked v-model="attribute2">
While this approach works well for achieving two-way binding in Vue, it does result in repetitive code.
Another potential method involves using the @change event to monitor checkbox changes, but this seems to offer only one-way binding. The Vue console shows updated values only upon panel refresh.
Are there any better techniques for implementing two-way binding in this specific scenario?