Afform - Fix showing default values for EntityRef fields
authorColeman Watts <coleman@civicrm.org>
Wed, 5 Apr 2023 16:37:31 +0000 (12:37 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 5 Apr 2023 16:37:31 +0000 (12:37 -0400)
There was some back-and-forth with this. #25374 removed the change() trigger, which fixed
the recursion bug described in https://lab.civicrm.org/dev/core/-/issues/4083 but broke
setting defaults, prompting #25465 which proposed to change it back.

This fix walks the line by triggering the change event but only when the value has actually changed.
Fixes dev/core#4110

ang/crmUi.js

index 309d512bdd690be8fe90fd66b167d44c24a629c6..3b5a1522ef72ef89ef9bdd6d6891cccc7c62abfb 100644 (file)
           };
 
           if (ctrl.ngModel) {
+            var oldValue;
             // Ensure widget is updated when model changes
             ctrl.ngModel.$render = function() {
               element.val(ctrl.ngModel.$viewValue || '');
+              // Trigger change so the Select2 renders the current value,
+              // but only if the value has actually changed (to avoid recursion)
+              if (!angular.equals(ctrl.ngModel.$viewValue, oldValue)) {
+                oldValue = ctrl.ngModel.$viewValue;
+                element.change();
+              }
             };
 
             // Copied from ng-list