Skip to content

Commit 79c55d8

Browse files
authored
Merge pull request yuche#401 from notechsolution/master
Add debounce to avoid fast HTTP query (may provide function for yuche#252)
2 parents cf39e6b + 35f54de commit 79c55d8

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/Typeahead.vue

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div style="position: relative"
3-
v-bind:class="{'open':showDropdown}"
3+
v-bind:class="{'open':showDropdown}"
44
>
55
<input type="text" class="form-control"
66
:placeholder="placeholder"
@@ -25,15 +25,22 @@
2525

2626
<script>
2727
import {getJSON, coerce} from './utils/utils.js'
28+
import _ from 'lodash'
2829
2930
let Vue = window.Vue
3031
3132
export default {
3233
created () {
3334
this.items = this.primitiveData
35+
this.debounceUpdate = _.debounce(function(that){
36+
getJSON(that.async + that.value).then(data => {
37+
that.items = (that.key ? data[that.key] : data).slice(0, that.limit)
38+
that.showDropdown = that.items.length > 0
39+
})
40+
}, this.debounce);
3441
},
3542
partials: {
36-
default: '<span v-html="item | highlight query"></span>'
43+
default: '<span v-html="item | highlight value"></span>'
3744
},
3845
props: {
3946
value: {
@@ -81,6 +88,11 @@ export default {
8188
},
8289
placeholder: {
8390
type: String
91+
},
92+
debounce:{
93+
type:Number,
94+
default:100,
95+
coerce: coerce.number
8496
}
8597
},
8698
data () {
@@ -119,10 +131,7 @@ export default {
119131
this.showDropdown = this.items.length > 0
120132
}
121133
if (this.async) {
122-
getJSON(this.async + this.value).then(data => {
123-
this.items = (this.key ? data[this.key] : data).slice(0, this.limit)
124-
this.showDropdown = this.items.length > 0
125-
})
134+
this.debounceUpdate(this)
126135
}
127136
},
128137
reset () {

0 commit comments

Comments
 (0)