Skip to content

Commit 4be07dd

Browse files
committed
Performance and Quality of Life
Now debouncing input from user, this should help performance when there is a lot of data. This delay can be controlled by the debounceMS option in the filter widget. The widget now watches for the input being reset by clicking the X in the search box.
1 parent c7cccca commit 4be07dd

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/jquery.multiselect.filter.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,34 @@
1616
(function($) {
1717
var rEscape = /[\-\[\]{}()*+?.,\\\^$|#\s]/g;
1818

19+
//Courtesy of underscore.js
20+
function debounce(func, wait, immediate) {
21+
var timeout;
22+
return function() {
23+
var context = this, args = arguments;
24+
var later = function() {
25+
timeout = null;
26+
if (!immediate) {
27+
func.apply(context, args);
28+
}
29+
};
30+
var callNow = immediate && !timeout;
31+
clearTimeout(timeout);
32+
timeout = setTimeout(later, wait);
33+
if (callNow) {
34+
func.apply(context, args);
35+
}
36+
};
37+
}
38+
1939
$.widget('ech.multiselectfilter', {
2040

2141
options: {
2242
label: 'Filter:',
2343
width: null, /* override default width set in css file (px). null will inherit */
2444
placeholder: 'Enter keywords',
25-
autoReset: false
45+
autoReset: false,
46+
debounceMS: 250
2647
},
2748

2849
_create: function() {
@@ -49,8 +70,8 @@
4970
e.preventDefault();
5071
}
5172
},
52-
keyup: $.proxy(this._handler, this),
53-
click: $.proxy(this._handler, this)
73+
input: $.proxy(debounce(this._handler, opts.debounceMS), this),
74+
search: $.proxy(this._handler, this)
5475
});
5576

5677
// cache input values for searching

tests/unit/filter.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515

1616
function searchFor(term) {
17-
input.val(term).trigger("keyup");
17+
input.val(term).trigger("search");
1818
}
1919

2020
function triggerClick() {
@@ -31,16 +31,16 @@
3131
module("filter widget - multiple select", {
3232
setup: function() {
3333
el = $('<select multiple>' +
34-
'<option></option>' +
35-
'<option value="foo">testffoooo</option>' +
36-
'<option value="bar">testbbaarr</option>' +
37-
'<option value=" baz ">testbbaazz</option>' +
38-
'<option value="qux">testquxtest</option>' +
39-
'<option value="10">ten</option>' +
40-
'<option value="100">one hundred</option>' +
41-
'<option value="5">five</option>' +
42-
'<option>a test with word boundaries</option>' +
43-
'<option>special regex !^$()//-|{}/: characters</option>' +
34+
'<option></option>' +
35+
'<option value="foo">testffoooo</option>' +
36+
'<option value="bar">testbbaarr</option>' +
37+
'<option value=" baz ">testbbaazz</option>' +
38+
'<option value="qux">testquxtest</option>' +
39+
'<option value="10">ten</option>' +
40+
'<option value="100">one hundred</option>' +
41+
'<option value="5">five</option>' +
42+
'<option>a test with word boundaries</option>' +
43+
'<option>special regex !^$()//-|{}/: characters</option>' +
4444
'</option>');
4545

4646
el.appendTo(document.body);

0 commit comments

Comments
 (0)