-
Notifications
You must be signed in to change notification settings - Fork 12
/
vue-suggestions.vue
75 lines (73 loc) · 1.6 KB
/
vue-suggestions.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<template>
<input type="text" v-model="value">
</template>
<script>
import $ from 'jquery';
import 'suggestions-jquery';
export default {
props: {
model: {
required: true
},
coordinates: {},
options: {
type: Object,
default: {
type: 'ADDRESS',
addon: 'none'
}
}
},
data() {
return {
value: '',
coords: {
latitude: '',
longitude: ''
},
}
},
mounted() {
this.callbacks = $.Callbacks();
this.value = this.model;
this.initSuggestion();
},
destroyed() {
this.destroySuggestion();
},
watch: {
coords: {
handler() {
this.$emit('update:coordinates', this.coords);
},
deep: true
},
value() {
this.$emit('update:model', this.value);
},
model() {
this.value = this.model;
}
},
methods: {
initSuggestion() {
this.callbacks.add(this.onSelect);
this.callbacks.add(this.options.onSelect || $.noop)
const options = Object.assign({}, this.options, {
onSelect: suggestion => this.callbacks.fire(suggestion)
});
$(this.$el).suggestions(options);
},
destroySuggestion() {
const plugin = $(this.$el).suggestions();
plugin.dispose();
},
onSelect(suggestion) {
this.value = suggestion.value;
const { geo_lat, geo_lon } = suggestion.data;
this.coords.latitude = geo_lat;
this.coords.longitude = geo_lon;
}
}
};
</script>