FIX: autocomplete quirks

This commit is contained in:
Sam 2014-07-05 11:44:51 +10:00
parent 6012d98983
commit 8298929b67
2 changed files with 27 additions and 6 deletions

View File

@ -232,6 +232,9 @@ $.fn.autocomplete = function(options) {
if (completeStart === null) return; if (completeStart === null) return;
if (r && r.then && typeof(r.then) === "function") { if (r && r.then && typeof(r.then) === "function") {
if (div) {
div.hide().remove();
}
r.then(updateAutoComplete); r.then(updateAutoComplete);
return; return;
} }

View File

@ -9,24 +9,34 @@ var cache = {};
var cacheTopicId = null; var cacheTopicId = null;
var cacheTime = null; var cacheTime = null;
var debouncedSearch = Discourse.debouncePromise(function(term, topicId, include_groups) { var currentTerm;
return Discourse.ajax('/users/search/users', {
var debouncedSearch = _.debounce(function(term, topicId, include_groups, resultsFn) {
Discourse.ajax('/users/search/users', {
data: { data: {
term: term, term: term,
topic_id: topicId, topic_id: topicId,
include_groups: include_groups include_groups: include_groups
} }
}).then(function (r) { }).then(function (r) {
cache[term] = r; cache[term] = r;
cacheTime = new Date(); cacheTime = new Date();
return r;
if(term === currentTerm){
resultsFn(r);
}
}); });
}, 200);
}, 300);
Discourse.UserSearch = { Discourse.UserSearch = {
search: function(options) { search: function(options) {
var term = options.term || ""; var term = options.term || "";
currentTerm = term;
var include_groups = options.include_groups || false; var include_groups = options.include_groups || false;
var exclude = options.exclude || []; var exclude = options.exclude || [];
var topicId = options.topicId; var topicId = options.topicId;
@ -45,6 +55,7 @@ Discourse.UserSearch = {
if (cacheTopicId !== topicId) { if (cacheTopicId !== topicId) {
cache = {}; cache = {};
} }
cacheTopicId = topicId; cacheTopicId = topicId;
var organizeResults = function(r) { var organizeResults = function(r) {
@ -73,10 +84,17 @@ Discourse.UserSearch = {
}; };
if (cache[term]) { if (cache[term]) {
organizeResults(cache[term]); // inject a delay to avoid too much repainting
setTimeout(function(){
if(term !== currentTerm) {
return;
}
organizeResults(cache[term]);
}, 300);
} else { } else {
debouncedSearch(term, topicId, include_groups).then(organizeResults); debouncedSearch(term, topicId, include_groups, organizeResults);
} }
return promise; return promise;
} }