MCPcopy
hub / github.com/angular-ui/ui-router / selectPreLink

Function selectPreLink

test/angular/1.4/angular.js:29212–29274  ·  view source on GitHub ↗
(scope, element, attr, ctrls)

Source from the content-addressed store, hash-verified

29210 };
29211
29212 function selectPreLink(scope, element, attr, ctrls) {
29213
29214 // if ngModel is not defined, we don't need to do anything
29215 var ngModelCtrl = ctrls[1];
29216 if (!ngModelCtrl) return;
29217
29218 var selectCtrl = ctrls[0];
29219
29220 selectCtrl.ngModelCtrl = ngModelCtrl;
29221
29222 // When the selected item(s) changes we delegate getting the value of the select control
29223 // to the `readValue` method, which can be changed if the select can have multiple
29224 // selected values or if the options are being generated by `ngOptions`
29225 element.on('change', function() {
29226 scope.$apply(function() {
29227 ngModelCtrl.$setViewValue(selectCtrl.readValue());
29228 });
29229 });
29230
29231 // If the select allows multiple values then we need to modify how we read and write
29232 // values from and to the control; also what it means for the value to be empty and
29233 // we have to add an extra watch since ngModel doesn't work well with arrays - it
29234 // doesn't trigger rendering if only an item in the array changes.
29235 if (attr.multiple) {
29236
29237 // Read value now needs to check each option to see if it is selected
29238 selectCtrl.readValue = function readMultipleValue() {
29239 var array = [];
29240 forEach(element.find('option'), function(option) {
29241 if (option.selected) {
29242 array.push(option.value);
29243 }
29244 });
29245 return array;
29246 };
29247
29248 // Write value now needs to set the selected property of each matching option
29249 selectCtrl.writeValue = function writeMultipleValue(value) {
29250 var items = new HashMap(value);
29251 forEach(element.find('option'), function(option) {
29252 option.selected = isDefined(items.get(option.value));
29253 });
29254 };
29255
29256 // we have to do it on each watch since ngModel watches reference, but
29257 // we need to work of an array, so we need to see if anything was inserted/removed
29258 var lastView, lastViewRef = NaN;
29259 scope.$watch(function selectMultipleWatch() {
29260 if (lastViewRef === ngModelCtrl.$viewValue && !equals(lastView, ngModelCtrl.$viewValue)) {
29261 lastView = shallowCopy(ngModelCtrl.$viewValue);
29262 ngModelCtrl.$render();
29263 }
29264 lastViewRef = ngModelCtrl.$viewValue;
29265 });
29266
29267 // If we are a multiple select then value is now a collection
29268 // so the meaning of $isEmpty changes
29269 ngModelCtrl.$isEmpty = function(value) {

Callers

nothing calls this directly

Calls 4

forEachFunction · 0.70
isDefinedFunction · 0.70
equalsFunction · 0.70
shallowCopyFunction · 0.70

Tested by

no test coverage detected