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

Commit 785c6a4

Browse files
committed
Fix: match label shows correctly even if selected item isn't on items list anymore and using single property binding
1 parent 9d1bd79 commit 785c6a4

File tree

2 files changed

+108
-6
lines changed

2 files changed

+108
-6
lines changed

src/select.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,13 +581,18 @@
581581
}
582582
return resultMultiple;
583583
}else{
584-
for (var i = data.length - 1; i >= 0; i--) {
584+
var checkFn = function(d){
585585
locals = {};
586-
locals[$select.parserResult.itemName] = data[i];
586+
locals[$select.parserResult.itemName] = d;
587587
result = $select.parserResult.modelMapper(scope, locals);
588-
if (result == inputValue){
589-
return data[i];
590-
}
588+
return result == inputValue;
589+
};
590+
//If possible pass same object stored in $select.selected
591+
if ($select.selected && checkFn($select.selected)) {
592+
return $select.selected;
593+
}
594+
for (var i = data.length - 1; i >= 0; i--) {
595+
if (checkFn(data[i])) return data[i];
591596
}
592597
}
593598
}

test/select.spec.js

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ describe('ui-select tests', function() {
109109
element.trigger(e);
110110
}
111111

112+
function setSearchText(el, text) {
113+
el.scope().$select.search = text;
114+
scope.$digest();
115+
$timeout.flush();
116+
}
117+
112118
// Tests
113119

114120
it('should compile child directives', function() {
@@ -558,7 +564,7 @@ describe('ui-select tests', function() {
558564
<div ng-bind-html="person.name | highlight: $select.search"></div> \
559565
<div ng-if="person.name==\'Wladimir\'"> \
560566
<span class="only-once">I should appear only once</span>\
561-
® </div> \
567+
</div> \
562568
</ui-select-choices> \
563569
</ui-select>'
564570
);
@@ -568,6 +574,97 @@ describe('ui-select tests', function() {
568574

569575
});
570576

577+
it('should call refresh function when search text changes', function () {
578+
579+
var el = compileTemplate(
580+
'<ui-select ng-model="selection.selected"> \
581+
<ui-select-match> \
582+
</ui-select-match> \
583+
<ui-select-choices repeat="person in people | filter: $select.search" \
584+
refresh="fetchFromServer($select.search)" refresh-delay="0"> \
585+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
586+
<div ng-if="person.name==\'Wladimir\'"> \
587+
<span class="only-once">I should appear only once</span>\
588+
</div> \
589+
</ui-select-choices> \
590+
</ui-select>'
591+
);
592+
593+
scope.fetchFromServer = function(){};
594+
595+
spyOn(scope, 'fetchFromServer');
596+
597+
el.scope().$select.search = 'r';
598+
scope.$digest();
599+
$timeout.flush();
600+
601+
expect(scope.fetchFromServer).toHaveBeenCalledWith('r');
602+
603+
});
604+
605+
it('should call refresh function when search text changes', function () {
606+
607+
var el = compileTemplate(
608+
'<ui-select ng-model="selection.selected"> \
609+
<ui-select-match> \
610+
</ui-select-match> \
611+
<ui-select-choices repeat="person in people | filter: $select.search" \
612+
refresh="fetchFromServer($select.search)" refresh-delay="0"> \
613+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
614+
<div ng-if="person.name==\'Wladimir\'"> \
615+
<span class="only-once">I should appear only once</span>\
616+
</div> \
617+
</ui-select-choices> \
618+
</ui-select>'
619+
);
620+
621+
scope.fetchFromServer = function(){};
622+
623+
spyOn(scope, 'fetchFromServer');
624+
625+
el.scope().$select.search = 'r';
626+
scope.$digest();
627+
$timeout.flush();
628+
629+
expect(scope.fetchFromServer).toHaveBeenCalledWith('r');
630+
631+
});
632+
633+
it('should format view value correctly when using single property binding and refresh funcion', function () {
634+
635+
var el = compileTemplate(
636+
'<ui-select ng-model="selection.selected"> \
637+
<ui-select-match>{{$select.selected.name}}</ui-select-match> \
638+
<ui-select-choices repeat="person.name as person in people | filter: $select.search" \
639+
refresh="fetchFromServer($select.search)" refresh-delay="0"> \
640+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
641+
<div ng-if="person.name==\'Wladimir\'"> \
642+
<span class="only-once">I should appear only once</span>\
643+
</div> \
644+
</ui-select-choices> \
645+
</ui-select>'
646+
);
647+
648+
scope.fetchFromServer = function(searching){
649+
650+
if (searching == 's')
651+
return scope.people
652+
653+
if (searching == 'o'){
654+
scope.people = []; //To simulate cases were previously selected item isnt in the list anymore
655+
}
656+
657+
};
658+
659+
setSearchText(el, 'r')
660+
clickItem(el, 'Samantha');
661+
expect(getMatchLabel(el)).toBe('Samantha');
662+
663+
setSearchText(el, 'o')
664+
expect(getMatchLabel(el)).toBe('Samantha');
665+
666+
});
667+
571668
describe('search-enabled option', function() {
572669

573670
var el;

0 commit comments

Comments
 (0)