CRM-18586 - Fix race condition when loading custom data forms
authorColeman Watts <coleman@civicrm.org>
Sat, 21 May 2016 14:53:07 +0000 (10:53 -0400)
committerColeman Watts <coleman@civicrm.org>
Sat, 21 May 2016 14:53:07 +0000 (10:53 -0400)
templates/CRM/Contact/Form/Contact.tpl
templates/CRM/Contact/Form/CustomData.tpl
templates/CRM/common/customData.tpl

index 5d1af722cbe853c91a1070e2650e9389aa80dee5..776d541932d86e16c5b4bd745d89d76b5d84cbb3 100644 (file)
     });
 
     $('.customDataPresent').change(function() {
-      //$('.crm-custom-accordion').remove();
       var values = $("#contact_sub_type").val();
-      var contactType = {/literal}"{$contactType}"{literal};
-      CRM.buildCustomData(contactType, values);
-      loadMultiRecordFields(values);
-      $('.crm-custom-accordion').each(function() {
+      CRM.buildCustomData({/literal}"{$contactType}"{literal}, values).one('crmLoad', function() {
         highlightTabs(this);
+        loadMultiRecordFields(values);
       });
     });
 
     function loadMultiRecordFields(subTypeValues) {
-      if (subTypeValues == false) {
-        var subTypeValues = null;
+      if (subTypeValues === false) {
+        subTypeValues = null;
       }
-        else if (!subTypeValues) {
-        var subTypeValues = {/literal}"{$paramSubType}"{literal};
+      else if (!subTypeValues) {
+        subTypeValues = {/literal}"{$paramSubType}"{literal};
+      }
+      function loadNextRecord(i, groupValue, groupCount) {
+        if (i < groupCount) {
+          CRM.buildCustomData({/literal}"{$contactType}"{literal}, subTypeValues, null, i, groupValue, true).one('crmLoad', function() {
+            highlightTabs(this);
+            loadNextRecord(i+1, groupValue, groupCount);
+          });
+        }
       }
       {/literal}
       {foreach from=$customValueCount item="groupCount" key="groupValue"}
       {if $groupValue}{literal}
-        for ( var i = 1; i < {/literal}{$groupCount}{literal}; i++ ) {
-          CRM.buildCustomData( {/literal}"{$contactType}"{literal}, subTypeValues, null, i, {/literal}{$groupValue}{literal}, true );
-        }
+        loadNextRecord(1, {/literal}{$groupValue}{literal}, {/literal}{$groupCount}{literal});
       {/literal}
       {/if}
       {/foreach}
index fdbc285055c47b057eb5c09c5da11c5f0c686e77..01775c8b16bf0e29e4317cac43c7f1445ab4f22b 100644 (file)
   {if $customValueCount}
     {literal}
     <script type="text/javascript">
-      var customValueCount = {/literal}"{$customValueCount}"{literal};
-      var groupID = {/literal}"{$groupID}"{literal};
-      var contact_type = {/literal}"{$contact_type}"{literal};
-      var contact_subtype = {/literal}"{$contact_subtype}"{literal};
-      CRM.buildCustomData( contact_type, contact_subtype );
-      for ( var i = 1; i < customValueCount; i++ ) {
-        CRM.buildCustomData( contact_type, contact_subtype, null, i, groupID, true );
-      }
+      CRM.$(function() {
+        {/literal}
+        var customValueCount = "{$customValueCount}",
+          groupID = "{$groupID}",
+          contact_type = "{$contact_type}",
+          contact_subtype = "{$contact_subtype}",
+          i = 1;
+        {literal}
+        // FIXME: This is pretty terrible. Loading each item at a time via ajax.
+        // Building the complete form in php with no ajax would be way more efficient.
+        function loadNextRecord() {
+          if (i < customValueCount) {
+            CRM.buildCustomData(contact_type, contact_subtype, null, i++, groupID, true).one('crmLoad', loadNextRecord);
+          }
+        }
+        CRM.buildCustomData(contact_type, contact_subtype).one('crmLoad', loadNextRecord);
+      });
     </script>
     {/literal}
   {/if}
index 71a737553f73044c325df63426e7fcc049fa3d9b..129e0555b518c2a761d9a116cff8812c917deb6c 100644 (file)
@@ -80,7 +80,7 @@
         fname += subName;
       }
 
-      CRM.loadPage(dataUrl, {target: fname});
+      return CRM.loadPage(dataUrl, {target: fname});
     };
   })(CRM.$);
 </script>