MCPcopy Index your code
hub / github.com/angular-ui/ui-router / selectPreLink

Function selectPreLink

test/angular/1.5/angular.js:31980–32042  ·  view source on GitHub ↗
(scope, element, attr, ctrls)

Source from the content-addressed store, hash-verified

31978 };
31979
31980 function selectPreLink(scope, element, attr, ctrls) {
31981
31982 // if ngModel is not defined, we don't need to do anything
31983 var ngModelCtrl = ctrls[1];
31984 if (!ngModelCtrl) return;
31985
31986 var selectCtrl = ctrls[0];
31987
31988 selectCtrl.ngModelCtrl = ngModelCtrl;
31989
31990 // When the selected item(s) changes we delegate getting the value of the select control
31991 // to the `readValue` method, which can be changed if the select can have multiple
31992 // selected values or if the options are being generated by `ngOptions`
31993 element.on('change', function() {
31994 scope.$apply(function() {
31995 ngModelCtrl.$setViewValue(selectCtrl.readValue());
31996 });
31997 });
31998
31999 // If the select allows multiple values then we need to modify how we read and write
32000 // values from and to the control; also what it means for the value to be empty and
32001 // we have to add an extra watch since ngModel doesn't work well with arrays - it
32002 // doesn't trigger rendering if only an item in the array changes.
32003 if (attr.multiple) {
32004
32005 // Read value now needs to check each option to see if it is selected
32006 selectCtrl.readValue = function readMultipleValue() {
32007 var array = [];
32008 forEach(element.find('option'), function(option) {
32009 if (option.selected) {
32010 array.push(option.value);
32011 }
32012 });
32013 return array;
32014 };
32015
32016 // Write value now needs to set the selected property of each matching option
32017 selectCtrl.writeValue = function writeMultipleValue(value) {
32018 var items = new HashMap(value);
32019 forEach(element.find('option'), function(option) {
32020 option.selected = isDefined(items.get(option.value));
32021 });
32022 };
32023
32024 // we have to do it on each watch since ngModel watches reference, but
32025 // we need to work of an array, so we need to see if anything was inserted/removed
32026 var lastView, lastViewRef = NaN;
32027 scope.$watch(function selectMultipleWatch() {
32028 if (lastViewRef === ngModelCtrl.$viewValue && !equals(lastView, ngModelCtrl.$viewValue)) {
32029 lastView = shallowCopy(ngModelCtrl.$viewValue);
32030 ngModelCtrl.$render();
32031 }
32032 lastViewRef = ngModelCtrl.$viewValue;
32033 });
32034
32035 // If we are a multiple select then value is now a collection
32036 // so the meaning of $isEmpty changes
32037 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