From 4709994e19d0726fe5b8ee5c0086528d1dae4543 Mon Sep 17 00:00:00 2001 From: colemanw Date: Fri, 26 Jan 2024 10:11:43 -0500 Subject: [PATCH] dev/core#4937 - Fix js error in formBuilder autocompletes Due to 5071a76 this field was fetching form values during every digest cycle and in some cases this was causing an infinite loop. Updated the field to only fetch values when needed. --- ext/afform/core/ang/af/afField.component.js | 8 ++++++-- ext/afform/core/ang/af/fields/EntityRef.html | 2 +- js/Common.js | 14 ++++++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ext/afform/core/ang/af/afField.component.js b/ext/afform/core/ang/af/afField.component.js index c8c56c6e72..80d1d1ed05 100644 --- a/ext/afform/core/ang/af/afField.component.js +++ b/ext/afform/core/ang/af/afField.component.js @@ -256,8 +256,12 @@ }; }; - ctrl.getAutocompleteFieldName = function() { - return ctrl.afFieldset.modelName + (ctrl.afJoin ? ('+' + ctrl.afJoin.entity) : '') + ':' + ctrl.fieldName; + ctrl.getAutocompleteParams = function() { + return { + formName: 'afform:' + ctrl.afFieldset.getFormName(), + fieldName: ctrl.afFieldset.getName() + ':' + ctrl.fieldName, + values: $scope.dataProvider.getFieldData() + }; }; $scope.getOptions = function () { diff --git a/ext/afform/core/ang/af/fields/EntityRef.html b/ext/afform/core/ang/af/fields/EntityRef.html index 28bd70cf82..57b830dc69 100644 --- a/ext/afform/core/ang/af/fields/EntityRef.html +++ b/ext/afform/core/ang/af/fields/EntityRef.html @@ -5,7 +5,7 @@ ng-model="getSetSelect" ng-model-options="{getterSetter: true}" crm-autocomplete="$ctrl.defn.fk_entity" - crm-autocomplete-params="{formName: 'afform:' + $ctrl.afFieldset.getFormName(), fieldName: $ctrl.afFieldset.getName() + ':' + $ctrl.fieldName, values: dataProvider.getFieldData()}" + crm-autocomplete-params="$ctrl.getAutocompleteParams" multi="$ctrl.defn.input_attrs.multiple" auto-open="$ctrl.defn.input_attrs.autoOpen" quick-add="$ctrl.defn.input_attrs.quickAdd" diff --git a/js/Common.js b/js/Common.js index 1b5c08f9d1..dd084d11e1 100644 --- a/js/Common.js +++ b/js/Common.js @@ -570,6 +570,12 @@ if (!CRM.vars) CRM.vars = {}; // Autocomplete based on APIv4 and Select2. $.fn.crmAutocomplete = function(entityName, apiParams, select2Options) { + function getApiParams() { + if (typeof apiParams === 'function') { + return apiParams(); + } + return apiParams || {}; + } if (entityName === 'destroy') { return $(this).off('.crmEntity').crmSelect2('destroy'); } @@ -578,8 +584,7 @@ if (!CRM.vars) CRM.vars = {}; const $el = $(this).off('.crmEntity'); let staticItems = getStaticOptions(select2Options.static), quickAddLinks = select2Options.quickAdd, - multiple = !!select2Options.multiple, - key = apiParams.key || 'id'; + multiple = !!select2Options.multiple; $el.crmSelect2(_.extend({ ajax: { @@ -589,7 +594,7 @@ if (!CRM.vars) CRM.vars = {}; return {params: JSON.stringify(_.assign({ input: input, page: pageNum || 1 - }, apiParams))}; + }, getApiParams()))}; }, results: function(data) { return { @@ -615,7 +620,7 @@ if (!CRM.vars) CRM.vars = {}; if (!idsNeeded.length) { callback(multiple ? existing : existing[0]); } else { - var params = $.extend({}, apiParams || {}, {ids: idsNeeded}); + var params = $.extend({}, getApiParams(), {ids: idsNeeded}); CRM.api4(entityName, 'autocomplete', params).then(function (result) { callback(multiple ? result.concat(existing) : result[0]); }); @@ -663,6 +668,7 @@ if (!CRM.vars) CRM.vars = {}; const response = data.submissionResponse && data.submissionResponse[0]; let createdId; if (typeof response === 'object') { + let key = getApiParams().key || 'id'; // Loop through entities created by the afform (there should be only one) Object.keys(response).forEach((entity) => { if (Array.isArray(response[entity]) && response[entity][0] && response[entity][0][key]) { -- 2.25.1