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

fix(#271) if option is set the refresh function is getting the request. #1845

Merged
merged 11 commits into from
Jan 25, 2017
11 changes: 7 additions & 4 deletions src/uiSelectChoicesDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ uis.directive('uiSelectChoices',


$select.parseRepeatAttr(attrs.repeat, groupByExp, groupFilterExp); //Result ready at $select.parserResult

$select.refreshOnActive = scope.$eval(attrs.refreshOnActive);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets remove the attribute, this can be default functionality. We just call refresh more often now. (ie. When search.length == 0)

$select.disableChoiceExpression = attrs.uiDisableChoice;
$select.onHighlightCallback = attrs.onHighlight;

$select.dropdownPosition = attrs.position ? attrs.position.toLowerCase() : uiSelectConfig.dropdownPosition;

scope.$on('$destroy', function() {
Expand All @@ -68,7 +67,7 @@ uis.directive('uiSelectChoices',
scope.$watch('$select.search', function(newValue) {
if(newValue && !$select.open && $select.multiple) $select.activate(false, true);
$select.activeIndex = $select.tagging.isActivated ? -1 : 0;
if (!attrs.minimumInputLength || $select.search.length >= attrs.minimumInputLength) {
if ((!attrs.minimumInputLength || $select.search.length >= attrs.minimumInputLength) && (!$select.refreshOnActive || ($select.refreshOnActive && $select.open))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can undo changes here also. It will be up to user to check in their own function.

$select.refresh(attrs.refresh);
} else {
$select.items = [];
Expand All @@ -80,10 +79,14 @@ uis.directive('uiSelectChoices',
var refreshDelay = scope.$eval(attrs.refreshDelay);
$select.refreshDelay = refreshDelay !== undefined ? refreshDelay : uiSelectConfig.refreshDelay;
});

scope.$watch('$select.open', function(open) {
if (open) {
tElement.attr('role', 'listbox');
if(!angular.isUndefined($select.refreshOnActive)){
Copy link
Contributor

@user378230 user378230 Nov 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder instead if we do this in a uis:activate listener? (So it's clearer)

//only add a watch when refreshonactive is set.
$select.refresh(attrs.refresh);
}
} else {
tElement.removeAttr('role');
}
Expand Down
1 change: 0 additions & 1 deletion src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ uis.controller('uiSelectCtrl',
ctrl.open = true;

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

// ensure that the index is set to zero for tagging variants
// that where first option is auto-selected
if ( ctrl.activeIndex === -1 && ctrl.taggingLabel !== false ) {
Expand Down
16 changes: 15 additions & 1 deletion test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ describe('ui-select tests', function() {
if (attrs.refresh !== undefined) { choicesAttrsHtml += ' refresh="' + attrs.refresh + '"'; }
if (attrs.refreshDelay !== undefined) { choicesAttrsHtml += ' refresh-delay="' + attrs.refreshDelay + '"'; }
if (attrs.backspaceReset !== undefined) { attrsHtml += ' backspace-reset="' + attrs.backspaceReset + '"';}
if (attrs.refreshOnActive !== undefined) { choicesAttrsHtml += ' refresh-on-active="' + attrs.refreshOnActive + '"'; }

}

return compileTemplate(
Expand Down Expand Up @@ -3119,5 +3121,17 @@ describe('ui-select tests', function() {
expect(el.scope().$select.spinnerClass).toBe('randomclass');
});
});


describe('With refresh on active', function(){
it('should not refresh untill is activated', function(){
scope.fetchFromServer = function(){};
var el = createUiSelect({refresh:"fetchFromServer($select.search)",refreshOnActive:true,refreshDelay:0});
spyOn(scope, 'fetchFromServer');
$timeout.flush();
expect(scope.fetchFromServer.calls.any()).toEqual(false);
el.scope().$select.activate();
$timeout.flush();
expect(scope.fetchFromServer.calls.any()).toEqual(true);
});
});
});