@@ -19,7 +19,8 @@ angular.module('ui.select', [])
19
19
20
20
. constant ( 'uiSelectConfig' , {
21
21
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
23
24
} )
24
25
25
26
// 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', [])
112
113
ctrl . open = false ;
113
114
ctrl . disabled = undefined ; // Initialized inside uiSelect directive link function
114
115
ctrl . resetSearchInput = undefined ; // Initialized inside uiSelect directive link function
116
+ ctrl . refreshDelay = undefined ; // Initialized inside choices directive link function
115
117
116
118
ctrl . searchInput = $element . querySelectorAll ( 'input.ui-select-search' ) ;
117
119
if ( ctrl . searchInput . length !== 1 ) {
@@ -153,16 +155,26 @@ angular.module('ui.select', [])
153
155
} ) ;
154
156
} ;
155
157
158
+ var _refreshDelayPromise = undefined ;
159
+
156
160
/**
157
161
* Typeahead mode: lets the user refresh the collection using his own function.
158
162
*
159
163
* See Expose $select.search for external / remote filtering https://github.com/angular-ui/ui-select/pull/31
160
164
*/
161
165
ctrl . refresh = function ( refreshAttr ) {
162
166
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 ( ) {
164
176
$scope . $apply ( refreshAttr ) ;
165
- } ) ;
177
+ } , ctrl . refreshDelay ) ;
166
178
}
167
179
} ;
168
180
@@ -367,6 +379,12 @@ angular.module('ui.select', [])
367
379
$select . activeIndex = 0 ;
368
380
$select . refresh ( attrs . refresh ) ;
369
381
} ) ;
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
+ } ) ;
370
388
} ;
371
389
}
372
390
} ;
0 commit comments