Clean up JS

This commit is contained in:
Robin Ward 2013-04-08 12:55:59 -04:00
parent 0f767ca227
commit 6c983218b3
2 changed files with 24 additions and 33 deletions

View File

@ -6,26 +6,22 @@
@module Discourse @module Discourse
@param {function} func The function to debounce @param {function} func The function to debounce
@param {Numbers} wait how long to wait @param {Numbers} wait how long to wait
@param {Boolean} trickle
**/ **/
Discourse.debounce = function(func, wait, trickle) { Discourse.debounce = function(func, wait) {
var timeout; var timeout = null;
timeout = null;
return function() { return function() {
var args, context, currentWait, later; var context = this;
context = this; var args = arguments;
args = arguments;
later = function() { var later = function() {
timeout = null; timeout = null;
return func.apply(context, args); return func.apply(context, args);
}; };
if (timeout && trickle) { if (timeout) return;
// already queued, let it through
return;
}
var currentWait;
if (typeof wait === "function") { if (typeof wait === "function") {
currentWait = wait(); currentWait = wait();
} else { } else {

View File

@ -5,15 +5,11 @@
@namespace Discourse @namespace Discourse
@module Discourse @module Discourse
**/ **/
var cache, cacheTime, cacheTopicId, debouncedSearch, doSearch; var cache = {};
var cacheTopicId = null;
var cacheTime = null;
cache = {}; var doSearch = function(term, topicId, success) {
cacheTopicId = null;
cacheTime = null;
doSearch = function(term, topicId, success) {
return Discourse.ajax({ return Discourse.ajax({
url: Discourse.getURL('/users/search/users'), url: Discourse.getURL('/users/search/users'),
dataType: 'JSON', dataType: 'JSON',
@ -29,16 +25,16 @@ doSearch = function(term, topicId, success) {
}); });
}; };
debouncedSearch = Discourse.debounce(doSearch, 200); var debouncedSearch = Discourse.debounce(doSearch, 200);
Discourse.UserSearch = { Discourse.UserSearch = {
search: function(options) { search: function(options) {
var callback, exclude, limit, success, term, topicId; var term = options.term || "";
term = options.term || ""; var callback = options.callback;
callback = options.callback; var exclude = options.exclude || [];
exclude = options.exclude || []; var topicId = options.topicId;
topicId = options.topicId; var limit = options.limit || 5;
limit = options.limit || 5;
if (!callback) { if (!callback) {
throw "missing callback"; throw "missing callback";
} }
@ -55,20 +51,18 @@ Discourse.UserSearch = {
cache = {}; cache = {};
} }
cacheTopicId = topicId; cacheTopicId = topicId;
success = function(r) { var success = function(r) {
var result; var result = [];
result = [];
r.users.each(function(u) { r.users.each(function(u) {
if (exclude.indexOf(u.username) === -1) { if (exclude.indexOf(u.username) === -1) {
result.push(u); result.push(u);
} }
if (result.length > limit) { if (result.length > limit) return false;
return false;
}
return true; return true;
}); });
return callback(result); return callback(result);
}; };
if (cache[term]) { if (cache[term]) {
success(cache[term]); success(cache[term]);
} else { } else {
@ -76,6 +70,7 @@ Discourse.UserSearch = {
} }
return true; return true;
} }
}; };