From 8298929b675cdb047978abe56b27aec1c9a5b738 Mon Sep 17 00:00:00 2001 From: Sam Date: Sat, 5 Jul 2014 11:44:51 +1000 Subject: [PATCH] FIX: autocomplete quirks --- .../javascripts/discourse/lib/autocomplete.js | 3 ++ .../javascripts/discourse/lib/user_search.js | 30 +++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/autocomplete.js b/app/assets/javascripts/discourse/lib/autocomplete.js index c3222cf3a3d..59adb766e7c 100644 --- a/app/assets/javascripts/discourse/lib/autocomplete.js +++ b/app/assets/javascripts/discourse/lib/autocomplete.js @@ -232,6 +232,9 @@ $.fn.autocomplete = function(options) { if (completeStart === null) return; if (r && r.then && typeof(r.then) === "function") { + if (div) { + div.hide().remove(); + } r.then(updateAutoComplete); return; } diff --git a/app/assets/javascripts/discourse/lib/user_search.js b/app/assets/javascripts/discourse/lib/user_search.js index 9bf1989c9da..ddd8524ceb7 100644 --- a/app/assets/javascripts/discourse/lib/user_search.js +++ b/app/assets/javascripts/discourse/lib/user_search.js @@ -9,24 +9,34 @@ var cache = {}; var cacheTopicId = null; var cacheTime = null; -var debouncedSearch = Discourse.debouncePromise(function(term, topicId, include_groups) { - return Discourse.ajax('/users/search/users', { +var currentTerm; + +var debouncedSearch = _.debounce(function(term, topicId, include_groups, resultsFn) { + + Discourse.ajax('/users/search/users', { data: { term: term, topic_id: topicId, include_groups: include_groups } }).then(function (r) { + cache[term] = r; cacheTime = new Date(); - return r; + + if(term === currentTerm){ + resultsFn(r); + } }); -}, 200); + +}, 300); Discourse.UserSearch = { search: function(options) { var term = options.term || ""; + currentTerm = term; + var include_groups = options.include_groups || false; var exclude = options.exclude || []; var topicId = options.topicId; @@ -45,6 +55,7 @@ Discourse.UserSearch = { if (cacheTopicId !== topicId) { cache = {}; } + cacheTopicId = topicId; var organizeResults = function(r) { @@ -73,10 +84,17 @@ Discourse.UserSearch = { }; 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 { - debouncedSearch(term, topicId, include_groups).then(organizeResults); + debouncedSearch(term, topicId, include_groups, organizeResults); } + return promise; }