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

Commit 33f4d9f

Browse files
committed
refresh on active
1 parent da159d4 commit 33f4d9f

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/uiSelectChoicesDirective.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ uis.directive('uiSelectChoices',
2727
$select.disableChoiceExpression = attrs.uiDisableChoice;
2828
$select.onHighlightCallback = attrs.onHighlight;
2929

30+
$select.refreshOnActive = scope.$eval(attrs.refreshOnActive);
31+
3032
if(groupByExp) {
3133
var groups = element.querySelectorAll('.ui-select-choices-group');
3234
if (groups.length !== 1) throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-group but got '{0}'.", groups.length);
@@ -52,7 +54,15 @@ uis.directive('uiSelectChoices',
5254
scope.$watch('$select.search', function(newValue) {
5355
if(newValue && !$select.open && $select.multiple) $select.activate(false, true);
5456
$select.activeIndex = $select.tagging.isActivated ? -1 : 0;
55-
$select.refresh(attrs.refresh);
57+
if(!$select.refreshOnActive || ($select.refreshOnActive && $select.refreshIsActive)) {
58+
$select.refresh(attrs.refresh);
59+
}
60+
});
61+
62+
scope.$watch('$select.refreshIsActive', function(newValue, oldValue){
63+
if(angular.isUndefined(oldValue) && newValue){
64+
$select.refresh(attrs.refresh);
65+
}
5666
});
5767

5868
attrs.$observe('refreshDelay', function() {

src/uiSelectController.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ uis.controller('uiSelectCtrl',
3434
ctrl.closeOnSelect = true; // Initialized inside uiSelect directive link function
3535
ctrl.clickTriggeredSelect = false;
3636
ctrl.$filter = $filter;
37+
ctrl.refreshOnActive = undefined;
38+
ctrl.refreshIsActive = undefined;
3739

3840
ctrl.isEmpty = function() {
3941
return angular.isUndefined(ctrl.selected) || ctrl.selected === null || ctrl.selected === '';
@@ -65,6 +67,8 @@ uis.controller('uiSelectCtrl',
6567

6668
ctrl.activeIndex = ctrl.activeIndex >= ctrl.items.length ? 0 : ctrl.activeIndex;
6769

70+
ctrl.refreshIsActive = true;
71+
6872
// ensure that the index is set to zero for tagging variants
6973
// that where first option is auto-selected
7074
if ( ctrl.activeIndex === -1 && ctrl.taggingLabel !== false ) {

test/select.spec.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ describe('ui-select tests', function() {
191191

192192
expect(getMatchLabel(el)).toEqual('Adam');
193193
});
194-
194+
195195
it('should correctly render initial state with track by feature', function() {
196196
var el = compileTemplate(
197197
'<ui-select ng-model="selection.selected"> \
@@ -1791,4 +1791,33 @@ describe('ui-select tests', function() {
17911791
}
17921792
});
17931793
});
1794+
1795+
describe('with refresh on active', function(){
1796+
it('should not refresh untill is activate', function(){
1797+
1798+
var el = compileTemplate(
1799+
'<ui-select ng-model="selection.selected"> \
1800+
<ui-select-match> \
1801+
</ui-select-match> \
1802+
<ui-select-choices repeat="person in people | filter: $select.search" \
1803+
refresh="fetchFromServer($select.search)" refresh-on-active="true" refresh-delay="0"> \
1804+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
1805+
<div ng-if="person.name==\'Wladimir\'"> \
1806+
<span class="only-once">I should appear only once</span>\
1807+
</div> \
1808+
</ui-select-choices> \
1809+
</ui-select>'
1810+
);
1811+
1812+
scope.fetchFromServer = function(){};
1813+
spyOn(scope, 'fetchFromServer');
1814+
$timeout.flush();
1815+
expect(scope.fetchFromServer.calls.any()).toEqual(false);
1816+
1817+
el.scope().$select.activate();
1818+
$timeout.flush();
1819+
expect(scope.fetchFromServer.calls.any()).toEqual(true);
1820+
});
1821+
1822+
});
17941823
});

0 commit comments

Comments
 (0)