From a38b88b2f7a41cd963bb946b165f6378a9a423fc Mon Sep 17 00:00:00 2001 From: Phillip Johnsen Date: Mon, 27 Aug 2012 22:17:19 +0200 Subject: [PATCH] - Made it possible to set a custom text displayed while searching - Preventing unnecessary search requests --- jquery.jsonSuggest-2.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/jquery.jsonSuggest-2.js b/jquery.jsonSuggest-2.js index 95fbd8e..d7d24da 100644 --- a/jquery.jsonSuggest-2.js +++ b/jquery.jsonSuggest-2.js @@ -72,9 +72,11 @@ highlightMatches: true, onSelect: undefined, width: undefined, - property: 'text' + property: 'text', + searchProgressText: 'Searching...' }, - getJSONTimeout; + getJSONTimeout, + lastQueryText; settings = $.extend(defaults, settings); return this.each(function() { @@ -107,7 +109,7 @@ */ function selectResultItem(item) { obj.val(item[settings.property]); - $(results).html('').hide(); + clearAndHideResults(); if (typeof settings.onSelect === 'function') { settings.onSelect(item); @@ -134,12 +136,14 @@ * on in the settings. */ function buildResults(resultObjects, filterTxt) { + lastQueryText = filterTxt; + filterTxt = '(' + filterTxt + ')'; var bOddRow = true, i, iFound = 0, filterPatt = settings.caseSensitive ? new RegExp(filterTxt, 'g') : new RegExp(filterTxt, 'ig'); - $(results).html('').hide(); + clearAndHideResults(); for (i = 0; i < resultObjects.length; i += 1) { var item = $('
  • '), @@ -194,7 +198,11 @@ * run a match against each item in the possible results and display any * results on the page allowing selection by the user. */ - function runSuggest(e) { + function runSuggest(e) { + if (!hasQueryTextChanged(this)) { + return; + } + var search = function(searchData) { if (this.value.length < settings.minCharacters) { clearAndHideResults(); @@ -235,7 +243,7 @@ return false; } - $(results).html('
  • Searching...
  • '). + $(results).html('
  • '+ settings.searchProgressText +'
  • '). show().css('height', 'auto'); getJSONTimeout = window.clearTimeout(getJSONTimeout); @@ -257,7 +265,14 @@ */ function clearAndHideResults() { $(results).html('').hide(); - } + } + + /** + * Whether or not the search query has changed since the last results was build + */ + function hasQueryTextChanged(domElement) { + return domElement.value !== lastQueryText; + } /** * To call specific actions based on the keys pressed in the input