diff --git a/template/app/GuideSearch.js b/template/app/GuideSearch.js index b5ac922435fbc22319d428a75b17a5f685545e2a..7863f1702679ece2ebfc360693037a8c5cb1ffcf 100644 --- a/template/app/GuideSearch.js +++ b/template/app/GuideSearch.js @@ -13,6 +13,29 @@ Ext.define("Docs.GuideSearch", { return !!Docs.data.guideSearch.url; }, + /** + * Invokes #search after a specified delay. + * + * @param {String} term The query string to search for + * @param {Function} callback Function to call with an array of search results. + * @param {Object} scope Scope for the function. + * @param {Number} delay Milliseconds to wait before starting. + */ + deferredSearch: function(term, callback, scope, delay) { + // When new search term comes in, cancel the previous delayed search. + clearTimeout(this.timeout); + + var timeout = this.timeout = Ext.Function.defer(function() { + this.search(term, function(results) { + // When new search is already started, don't show the + // results of old search. + if (timeout === this.timeout) { + callback.call(scope, results); + } + }, this); + }, delay, this); + }, + /** * Peforms the search remotely, then calls the given function. * diff --git a/template/app/controller/Search.js b/template/app/controller/Search.js index 99db87eab2bfeb08ec58e3e12f403c44d78d68f7..636ace34e1e9d7c6097061c7bcc7066271ec7a85 100644 --- a/template/app/controller/Search.js +++ b/template/app/controller/Search.js @@ -151,19 +151,10 @@ Ext.define('Docs.controller.Search', { } }, - // Wait a bit before launching guides search. - // When new search term comes in, cancel the previous delayed search. - // When the search had already started, don't show the results when it finishes. guideSearch: function(term) { - clearTimeout(this.guideSearchTimeout); - - var timeout = this.guideSearchTimeout = Ext.Function.defer(function() { - Docs.GuideSearch.search(term, function(guideResults) { - if (timeout === this.guideSearchTimeout) { - this.basicSearch(term, guideResults); - } - }, this); - }, this.guideSearchDelay, this); + Docs.GuideSearch.deferredSearch(term, function(guideResults) { + this.basicSearch(term, guideResults); + }, this, this.guideSearchDelay); }, basicSearch: function(term, guideResults) {