I'm using select2 to enhance an html select element. I want to bind the value of the select element to a Vue variable, however the Select2 seems to be preventing this.
What's the best way to achieve this data binding and event listening whilst retaining the Select2 behaviour. I imagine it's a case of linking the select2 events to the Vue instance.
I've made a fiddle demonstrating the problem (does not run below but works on jsfiddle):
$('#things').select2();
new Vue({
el: "#vue-example",
data: {
thing: null,
thing2: null
},
methods: {
log: function(str) {
$('#log').append(str + "<br>");
}
}
});
select {
width: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.12.8/vue.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="vue-example">
<label>Select2 Select Box</label>
<select name="things" id="things" v-model="thing" v-on="change: log('you changed thing1')">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
<br>
<br>
<label>Native Select Box</label>
<select name="things" id="things" v-model="thing2" v-on="change: log('you changed thing2')">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
<pre>{{ $data | json }}</pre>
<div id="log">
</div>
</div>