Skip to content
Snippets Groups Projects
Commit 339fa324 authored by Jared Hancock's avatar Jared Hancock
Browse files

typeahead: Implement `delay` option

This will allow the typeahead to wait a short amount of time before fetching
results from the backend, which will allow for request debouncing per se. In
other words, not every keystroke will signal a request to the backend to
update the results. Instead, when the user slows down typing, the request
will begin thereafter.
parent fc6420d6
Branches
Tags
No related merge requests found
......@@ -34,6 +34,7 @@
this.onselect = this.options.onselect
this.strings = true
this.shown = false
this.deferred = null
this.listen()
}
......@@ -78,6 +79,11 @@
return this
}
, fetch: function() {
var value = this.source(this, this.query)
if (value) this.process(value)
}
, lookup: function (event) {
var that = this
, items
......@@ -87,8 +93,9 @@
this.query = this.$element.val();
/*Check if we have a match on the current source?? */
if (typeof this.source == "function") {
value = this.source(this, this.query)
if (value) this.process(value)
if (!this.options.delay) return this.fetch()
if (this.deferred) clearTimeout(this.deferred)
this.deferred = setTimeout(this.fetch.bind(this), this.options.delay)
} else {
this.process(this.source)
}
......@@ -337,6 +344,7 @@
, render: 'info'
, minLength: 1
, scroll: false
, delay: 200
}
$.fn.typeahead.Constructor = Typeahead
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment