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