From e3e27bb0668c6ecf4f1eed7a5aecf8b5048894b0 Mon Sep 17 00:00:00 2001 From: Tomas Kirda Date: Fri, 7 Jun 2013 19:21:24 -0500 Subject: [PATCH] Implement selection invalidation. --- scripts/demo.js | 3 +++ src/jquery.autocomplete.js | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/scripts/demo.js b/scripts/demo.js index b49d4b2..cab945c 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -47,6 +47,9 @@ $(function () { }, onHint: function (hint) { $('#autocomplete-ajax-x').val(hint); + }, + onInvalidateSelection: function() { + $('#selction-ajax').html('You selected: none'); } }); diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index 887a0f8..75a4fa6 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -100,6 +100,7 @@ }; that.hint = null; that.hintValue = ''; + that.selection = null; // Initialize and set options: that.initialize(); @@ -178,6 +179,7 @@ that.el.on('keyup.autocomplete', function (e) { that.onKeyUp(e); }); that.el.on('blur.autocomplete', function () { that.onBlur(); }); that.el.on('focus.autocomplete', function () { that.fixPosition(); }); + that.el.on('change.autocomplete', function (e) { that.onKeyUp(e); }); }, onBlur: function () { @@ -347,11 +349,10 @@ return; } - that.findBestHint(); - clearInterval(that.onChangeInterval); if (that.currentValue !== that.el.val()) { + that.findBestHint(); if (that.options.deferRequestBy > 0) { // Defer lookup in case when value changes very quickly: that.onChangeInterval = setInterval(function () { @@ -367,6 +368,11 @@ var that = this, q; + if (that.selection) { + that.selection = null; + (that.options.onInvalidateSelection || $.noop)(); + } + clearInterval(that.onChangeInterval); that.currentValue = that.el.val(); @@ -654,6 +660,7 @@ that.el.val(that.currentValue); that.signalHint(null); that.suggestions = []; + that.selection = suggestion; if ($.isFunction(onSelectCallback)) { onSelectCallback.call(that.element, suggestion);