CRM-10693 - AJAX - Standardize protocol for page, form, messages
[civicrm-core.git] / js / Common.js
index e5d14fef7f37005db8ebaf423dc1a317059f2d91..21dca3559eb8042dd686f3a7637a30034d21bd27 100644 (file)
@@ -820,6 +820,22 @@ CRM.validate = CRM.validate || {
     });
   }
 
+  // Preprocess all cj ajax calls to display messages
+  $(document).ajaxSuccess(function(event, xhr, settings) {
+    try {
+      if ((!settings.dataType || settings.dataType == 'json') && xhr.responseText) {
+        var response = $.parseJSON(xhr.responseText);
+        if (typeof(response.crmMessages) == 'object') {
+          $.each(response.crmMessages, function(n, msg) {
+            CRM.alert(msg.text, msg.title, msg.type, msg.options);
+          })
+        }
+      }
+    }
+    // Suppress errors
+    catch (e) {}
+  });
+
   $(function () {
     if ($('#crm-notification-container').length) {
       // Initialize notifications