FIX: username autocomplete more reliable, cancel old reqs

This commit is contained in:
Sam 2014-08-26 11:25:13 +10:00
parent 96af48d903
commit f331855482
1 changed files with 17 additions and 3 deletions

View File

@ -3,7 +3,8 @@ import { CANCELLED_STATUS } from 'discourse/lib/autocomplete';
var cache = {}, var cache = {},
cacheTopicId, cacheTopicId,
cacheTime, cacheTime,
currentTerm; currentTerm,
oldSearch;
function performSearch(term, topicId, includeGroups, resultsFn) { function performSearch(term, topicId, includeGroups, resultsFn) {
var cached = cache[term]; var cached = cache[term];
@ -12,18 +13,25 @@ function performSearch(term, topicId, includeGroups, resultsFn) {
return true; return true;
} }
Discourse.ajax('/users/search/users', { // need to be able to cancel this
oldSearch = $.ajax(Discourse.getURL('/users/search/users'), {
data: { term: term, data: { term: term,
topic_id: topicId, topic_id: topicId,
include_groups: includeGroups } include_groups: includeGroups }
}).then(function (r) { });
oldSearch.then(function (r) {
cache[term] = r; cache[term] = r;
cacheTime = new Date(); cacheTime = new Date();
// If there is a newer search term, return null // If there is a newer search term, return null
if (term !== currentTerm) { r = CANCELLED_STATUS; } if (term !== currentTerm) { r = CANCELLED_STATUS; }
resultsFn(r); resultsFn(r);
}).always(function(){
oldSearch = null;
}); });
return true; return true;
} }
var debouncedSearch = _.debounce(performSearch, 300); var debouncedSearch = _.debounce(performSearch, 300);
@ -69,6 +77,12 @@ export default function userSearch(options) {
includeGroups = !!options.include_groups, includeGroups = !!options.include_groups,
topicId = options.topicId; topicId = options.topicId;
if (oldSearch) {
oldSearch.abort();
oldSearch = null;
}
currentTerm = term; currentTerm = term;
return new Ember.RSVP.Promise(function(resolve) { return new Ember.RSVP.Promise(function(resolve) {