Skip to content

Commit

Permalink
[#1259] Fixes a JS error with autocomplete module and improves UX
Browse files Browse the repository at this point in the history
1. Basically adds an extra check to make sure that 'abort()' is
defined
2. Then changes the module to behave a little nicer when inbetween
ajax requests
  • Loading branch information
johnmartin committed Oct 3, 2013
1 parent ae5a51c commit afc80ed
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 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,30 @@ 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, function (terms) {
fn(terms);
});
}, 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 afc80ed

Please sign in to comment.