Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 1e580d5

Browse files
committed
Add refreshDelay attribute + debouncing
1 parent c43cc91 commit 1e580d5

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/select.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ angular.module('ui.select', [])
1919

2020
.constant('uiSelectConfig', {
2121
theme: 'bootstrap',
22-
placeholder: '' // Empty by default, like HTML tag <select>
22+
placeholder: '', // Empty by default, like HTML tag <select>
23+
refreshDelay: 1000 // In milliseconds
2324
})
2425

2526
// See Rename minErr and make it accessible from outside https://github.com/angular/angular.js/issues/6913
@@ -112,6 +113,7 @@ angular.module('ui.select', [])
112113
ctrl.open = false;
113114
ctrl.disabled = undefined; // Initialized inside uiSelect directive link function
114115
ctrl.resetSearchInput = undefined; // Initialized inside uiSelect directive link function
116+
ctrl.refreshDelay = undefined; // Initialized inside choices directive link function
115117

116118
ctrl.searchInput = $element.querySelectorAll('input.ui-select-search');
117119
if (ctrl.searchInput.length !== 1) {
@@ -153,16 +155,26 @@ angular.module('ui.select', [])
153155
});
154156
};
155157

158+
var _refreshDelayPromise = undefined;
159+
156160
/**
157161
* Typeahead mode: lets the user refresh the collection using his own function.
158162
*
159163
* See Expose $select.search for external / remote filtering https://github.com/angular-ui/ui-select/pull/31
160164
*/
161165
ctrl.refresh = function(refreshAttr) {
162166
if (refreshAttr !== undefined) {
163-
$timeout(function() {
167+
168+
// Throttle / debounce
169+
//
170+
// See https://github.com/angular-ui/bootstrap/blob/0d4c2e21c3/src/typeahead/typeahead.js#L162
171+
// FYI AngularStrap typeahead does not have debouncing: https://github.com/mgcrea/angular-strap/blob/1529ab4bbc/src/typeahead/typeahead.js#L172
172+
if (_refreshDelayPromise) {
173+
$timeout.cancel(_refreshDelayPromise);
174+
}
175+
_refreshDelayPromise = $timeout(function() {
164176
$scope.$apply(refreshAttr);
165-
});
177+
}, ctrl.refreshDelay);
166178
}
167179
};
168180

@@ -367,6 +379,12 @@ angular.module('ui.select', [])
367379
$select.activeIndex = 0;
368380
$select.refresh(attrs.refresh);
369381
});
382+
383+
attrs.$observe('refreshDelay', function() {
384+
// $eval() is needed otherwise we get a string instead of a number
385+
var refreshDelay = scope.$eval(attrs.refreshDelay);
386+
$select.refreshDelay = refreshDelay !== undefined ? refreshDelay : uiSelectConfig.refreshDelay;
387+
});
370388
};
371389
}
372390
};

0 commit comments

Comments
 (0)