Skip to content

Commit

Permalink
Merge pull request #1259 from okfn/1259-autocomplete-fixes
Browse files Browse the repository at this point in the history
Autocomplete JS module issues
  • Loading branch information
vitorbaptista committed Oct 15, 2013
2 parents e6d2c45 + db86eac commit 306ef40
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions ckan/public/base/javascript/modules/autocomplete.js
Expand Up @@ -86,6 +86,8 @@ this.ckan.module('autocomplete', function (jQuery, _) {
$('.select2-choice', select2.container).on('click', function() {
return false;
});

this._select2 = select2;
},

/* Looks up the completions for the current search term and passes them
Expand Down Expand Up @@ -140,29 +142,28 @@ this.ckan.module('autocomplete', function (jQuery, _) {
// old data.
this._lastTerm = string;

// Kills previous timeout
clearTimeout(this._debounced);

// OK, wipe the dropdown before we start ajaxing the completions
fn({results:[]});

if (string) {
if (!this._debounced) {
// Set a timer to prevent the search lookup occurring too often.
this._debounced = setTimeout(function () {
var term = module._lastTerm;
// Set a timer to prevent the search lookup occurring too often.
this._debounced = setTimeout(function () {
var term = module._lastTerm;

delete module._debounced;
// Cancel the previous request if it hasn't yet completed.
if (module._last && typeof module._last.abort == 'function') {
module._last.abort();
}

// Cancel the previous request if it hasn't yet completed.
if (module._last) {
module._last.abort();
}
module._last = module.getCompletions(term, fn);
}, this.options.interval);

module._last = module.getCompletions(term, function (terms) {
fn(module._lastResults = terms);
});
}, this.options.interval);
} else {
// Re-use the last set of terms.
fn(this._lastResults || {results: []});
}
} else {
fn({results: []});
// This forces the ajax throbber to appear, because we've called the
// callback already and that hides the throbber
$('.select2-search input', this._select2.dropdown).addClass('select2-active');
}
},

Expand Down

0 comments on commit 306ef40

Please sign in to comment.