From 339fa324855b65a031983b0a2f5dc7d15fc214e8 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Mon, 6 Jul 2015 15:58:21 -0500 Subject: [PATCH] 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. --- scp/js/bootstrap-typeahead.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scp/js/bootstrap-typeahead.js b/scp/js/bootstrap-typeahead.js index 7ddc01f61..d53630e4b 100644 --- a/scp/js/bootstrap-typeahead.js +++ b/scp/js/bootstrap-typeahead.js @@ -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 -- GitLab