Skip to content

Instantly share code, notes, and snippets.

@maynagashev
Last active April 30, 2017 18:45
Show Gist options
  • Save maynagashev/db7af8df8f4ea422726be59c20838381 to your computer and use it in GitHub Desktop.
Save maynagashev/db7af8df8f4ea422726be59c20838381 to your computer and use it in GitHub Desktop.
wrapper component with loglevel
<select2 :options="brands" v-model="form.brand_id" class="form-control" id="product_brand" name="product[brand_id]">
<option value="" selected="true" disabled="disabled">Выбeрите бренд из списка</option>
</select2>
<!-- created by maynagashev on 01.05.17 -->
<!-- ref: https://vuejs.org/v2/examples/select2.html -->
<template>
<select>
<slot></slot>
</select>
</template>
<script type="text/babel">
let ns = 'select2';
let log = window.log.getLogger(ns);
log.setLevel('debug');
export default {
props: ['options', 'value'],
mounted: function () {
log.debug('[mounted] init with options', this.options);
var vm = this;
// init select2
$(this.$el)
.select2({ data: this.options })
.val(this.value)
.trigger('change')
// emit event on change.
.on('change', function () {
vm.$emit('input', this.value)
})
},
watch: {
value: function (value) {
// update value
$(this.$el).val(value).trigger('change');
},
options: function (options) {
log.warn("[watch] options: ", options);
// update options
$(this.$el).select2({ data: options })
}
},
destroyed: function () {
$(this.$el).off().select2('destroy')
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment