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

Function selectPreLink

lib/test/angular/1.5.0/angular.js:29791–29853  ·  view source on GitHub ↗
(scope, element, attr, ctrls)

Source from the content-addressed store, hash-verified

29789 };
29790
29791 function selectPreLink(scope, element, attr, ctrls) {
29792
29793 // if ngModel is not defined, we don't need to do anything
29794 var ngModelCtrl = ctrls[1];
29795 if (!ngModelCtrl) return;
29796
29797 var selectCtrl = ctrls[0];
29798
29799 selectCtrl.ngModelCtrl = ngModelCtrl;
29800
29801 // When the selected item(s) changes we delegate getting the value of the select control
29802 // to the `readValue` method, which can be changed if the select can have multiple
29803 // selected values or if the options are being generated by `ngOptions`
29804 element.on('change', function() {
29805 scope.$apply(function() {
29806 ngModelCtrl.$setViewValue(selectCtrl.readValue());
29807 });
29808 });
29809
29810 // If the select allows multiple values then we need to modify how we read and write
29811 // values from and to the control; also what it means for the value to be empty and
29812 // we have to add an extra watch since ngModel doesn't work well with arrays - it
29813 // doesn't trigger rendering if only an item in the array changes.
29814 if (attr.multiple) {
29815
29816 // Read value now needs to check each option to see if it is selected
29817 selectCtrl.readValue = function readMultipleValue() {
29818 var array = [];
29819 forEach(element.find('option'), function(option) {
29820 if (option.selected) {
29821 array.push(option.value);
29822 }
29823 });
29824 return array;
29825 };
29826
29827 // Write value now needs to set the selected property of each matching option
29828 selectCtrl.writeValue = function writeMultipleValue(value) {
29829 var items = new HashMap(value);
29830 forEach(element.find('option'), function(option) {
29831 option.selected = isDefined(items.get(option.value));
29832 });
29833 };
29834
29835 // we have to do it on each watch since ngModel watches reference, but
29836 // we need to work of an array, so we need to see if anything was inserted/removed
29837 var lastView, lastViewRef = NaN;
29838 scope.$watch(function selectMultipleWatch() {
29839 if (lastViewRef === ngModelCtrl.$viewValue && !equals(lastView, ngModelCtrl.$viewValue)) {
29840 lastView = shallowCopy(ngModelCtrl.$viewValue);
29841 ngModelCtrl.$render();
29842 }
29843 lastViewRef = ngModelCtrl.$viewValue;
29844 });
29845
29846 // If we are a multiple select then value is now a collection
29847 // so the meaning of $isEmpty changes
29848 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