From: Coleman Watts Date: Wed, 5 Apr 2023 16:37:31 +0000 (-0400) Subject: Afform - Fix showing default values for EntityRef fields X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6f06261daeac8a93fb8b7db32c23d3572fa97975;p=civicrm-core.git Afform - Fix showing default values for EntityRef fields 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 --- diff --git a/ang/crmUi.js b/ang/crmUi.js index 309d512bdd..3b5a1522ef 100644 --- a/ang/crmUi.js +++ b/ang/crmUi.js @@ -751,9 +751,16 @@ }; 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