I'm using a Vuejs 1.0 directive to turn a select field (single and multiple) into a Select2 jQuery plugin field.
Vue.directive('select2', {
twoWay: true,
priority: 1000,
params: ['max-items'],
bind: function () {
var self = this;
console.log(self.params);
$(this.el)
.select2({
maximumSelectionLength: self.params.maxItems,
theme: 'bootstrap',
closeOnSelect: true
})
.on('change', function () {
var i, len, option, ref, values;
if (self.el.hasAttribute('multiple')) {
values = [];
ref = self.el.selectedOptions;
for (i = 0, len = ref.length; i < len; i++) {
option = ref[i];
values.push(option.value);
}
return self.set(values);
} else {
return self.set(self.el.value);
}
})
},
update: function (value, oldValue) {
$(this.el).val(value).trigger('change')
},
unbind: function () {
$(this.el).off().select2('destroy')
}
});
This all works fine. I'm also trying to bind a model to the value of the field but can't seem to get it to bind properly.
<select class="form-control" name="genre" v-model="upload.genre" v-select2="">
<option value="50">Abstract</option>
<option value="159">Acapella</option>
<option value="80">Acid</option>
...
</select>
The upload.genre property does not update automatically.