I have created a new component:
<template>
<div>
<select ref='select'>
<slot></slot>
</select>
</div>
</template>
<script>
export default {
props: ['options', 'value'],
mounted: function () {
var vm = this;
$(this.$refs.select)
.select2({
data: this.options,
tags: true,
createTag: function (params) {
return {
id: params.term,
text: params.term,
newOption: true
}
},
templateResult: function (data) {
var $result = $("<span></span>");
$result.text(data.text);
if (data.newOption) {
$result.append(" <em>(new)</em>");
}
return $result;
}
})
.on('change', function (ev, args) {
if (!(args && "ignore" in args)) {
vm.$emit('input', $(this).val());
}
});
Vue.nextTick(() => {
$(this.$refs.select)
.val(this.value)
.trigger('change', { ignore: true })
});
},
watch: {
value: function (value, oldValue) {
// update value
$(this.$refs.select)
.val(this.value)
.trigger('change', { ignore: true });
},
options: function (options) {
// update options
$(this.$refs.select).select2({ data: options })
}
},
destroyed: function () {
$(this.$refs.select).off().select2('destroy')
}
}
</script>
<style>
.select2-container--default {
min-width: 300px;
max-width: 500px;
}
</style>
And I am calling it in the parent.
My question is where am I listening for the event? here I am firing an event but if I am listening for it in the parent this event will fire all the parent listeners.
I have a page with 5 selects. So I am rendering them all in the page and then they all are having the same listener and when one of the select fires that it holds a value all of them are trggired. So if I select in the first one the first value then all the select would have the same value.
How can I properly need to listen for the event that I am firing in the component.
Thanks