Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(select): multiselect failes to update view on selection insert
Browse files Browse the repository at this point in the history
In multiselect when the underlying selection array push/pops an element the view did not re-render since the array reference stayed the same.
  • Loading branch information
mhevery committed Mar 19, 2012
1 parent 823adb2 commit 6ecac8e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,23 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
}

function Multiple(scope, selectElement, ctrl) {
var lastView;
ctrl.$render = function() {
var items = new HashMap(ctrl.$viewValue);
forEach(selectElement.children(), function(option) {
option.selected = isDefined(items.get(option.value));
});
};

// we have to do it on each watch since ng-model watches reference, but
// we need to work of an array, so we need to see if anything was inserted/removed
scope.$watch(function() {
if (!equals(lastView, ctrl.$viewValue)) {
lastView = copy(ctrl.$viewValue);
ctrl.$render();
}
});

selectElement.bind('change', function() {
scope.$apply(function() {
var array = [];
Expand Down
10 changes: 9 additions & 1 deletion test/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,15 @@ describe('select', function() {
scope.selection = ['A'];
});

expect(element[0].childNodes[0].selected).toEqual(true);
expect(element.find('option')[0].selected).toEqual(true);
expect(element.find('option')[1].selected).toEqual(false);

scope.$apply(function() {
scope.selection.push('B');
});

expect(element.find('option')[0].selected).toEqual(true);
expect(element.find('option')[1].selected).toEqual(true);
});


Expand Down

0 comments on commit 6ecac8e

Please sign in to comment.