CRM-14093 - prefetch searchBuilder options in bulk using CRM.api3
authorColeman Watts <coleman@civicrm.org>
Mon, 20 Jan 2014 01:14:59 +0000 (17:14 -0800)
committerColeman Watts <coleman@civicrm.org>
Mon, 20 Jan 2014 04:29:06 +0000 (20:29 -0800)
templates/CRM/Contact/Form/Search/Builder.js

index d5d19e97714682a3eb86d47f7f4d2dfe8e72fada..5f641ce0d905e9078f4a5adf5510f5efb11ccde0 100644 (file)
   ;
 
   $().crmAccordions();
-  $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField);
+
+  // Fetch initial options during page refresh - it's more efficient to bundle them in a single ajax request
+  var initialFields = {}, fetchFields = false;
+  $('select[id^=mapper][id$="_1"] option:selected', '#Builder').each(function() {
+    var field = $(this).attr('value');
+    if (typeof(CRM.searchBuilder.fieldOptions[field]) == 'string') {
+      initialFields[field] = [CRM.searchBuilder.fieldOptions[field], 'getoptions', {field: field, sequential: 1}];
+      fetchFields = true;
+    }
+  });
+  if (fetchFields) {
+    CRM.api3(initialFields).done(function(data) {
+      $.each(data, function(field, result) {
+        CRM.searchBuilder.fieldOptions[field] = result.values;
+      });
+      $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField);
+    });
+  } else {
+    $('select[id^=mapper][id$="_1"]', '#Builder').each(handleUserInputField);
+  }
 })(cj, CRM);