Skip to content

Commit

Permalink
Updating example.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksykulev committed Feb 9, 2014
1 parent 9d9f1b8 commit 99c563d
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions example/chosen.ajaxaddition.jquery.js
Expand Up @@ -17,7 +17,8 @@
throttle = false,
requestQueue = [],
typing = false,
loadingImg = '/img/loading.gif';
loadingImg = '/img/loading.gif',
minLength = 1;

if ($('option', select).length === 0) {
//adding empty option so you don't have to, and chosen can perform search correctly
Expand All @@ -42,7 +43,8 @@
requestQueueLength = requestQueue.length,
old = false,
keep = false,
inputEmptied = false;
inputEmptied = false,
inputEmptiedValue = '';
if (typing) {
//server returned a response, but it's about to become an older response
//so discard it and wait until the user is done typing
Expand Down Expand Up @@ -75,8 +77,9 @@
return false;
}
}
//while the request was processing did the user empty the input box
inputEmptied = $.trim(input.val()).length === 0;
//while the request was processing did the user "empty" the input box
inputEmptiedValue = $.trim(input.val());//if we have a minLength > 1 then we have to preserve the value (TODO::watch out for potential XSS/other breakages)
inputEmptied = inputEmptiedValue.length < minLength;

//if additional processing needs to occur on the returned json
if ('processItems' in options && $.isFunction(options.processItems)) {
Expand Down Expand Up @@ -129,7 +132,7 @@
keyRight = $.Event('keyup');
keyRight.which = 39;
//highlight
input.val(!inputEmptied ? data.q : '').trigger(keyRight).get(0).style.background = inputBG;
input.val(!inputEmptied ? data.q : inputEmptiedValue).trigger(keyRight).get(0).style.background = inputBG;

if (items.length > 0) {
$('.no-results', chosen).hide();
Expand All @@ -147,6 +150,10 @@
if ('loadingImg' in options) {
loadingImg = options.loadingImg;
}
//set minimum length that will trigger autocomplete
if ('minLength' in options) {
minLength = options.minLength;
}

$('.chosen-search > input, .chosen-choices .search-field input', chosen).
bind('keyup', processValue).
Expand Down Expand Up @@ -193,16 +200,17 @@
if ('useAjax' in options && $.isFunction(options.useAjax)) {
if (!options.useAjax(e)) { return false; }
}
//hide no results
$('.no-results', chosen).hide();
//backout if nothing is in input box
if ($.trim(q).length === 0) {
if ($.trim(q).length < minLength) {
input.get(0).style.background = inputBG;
if (throttle) { clearTimeout(throttle); }
return false;
}

typing = true;

//hide no results
$('.no-results', chosen).hide();
//add query to data
if ($.isArray(ajaxOptions.data)) {
//array
Expand Down

0 comments on commit 99c563d

Please sign in to comment.