Cleanup widgets to avoid memory leaks
[civicrm-core.git] / templates / CRM / common / enableDisableApi.tpl
index 63beb1fdcc9cdfeebabb7b3367402d0f9e74e8e1..7dd9811a00da9686bfd766c91e79138e8fc8c58e 100644 (file)
   cj(function($) {
     var $row, $table, info, enabled, fieldLabel;
 
+    function successMsg() {
+      {/literal} {* client-side variable substitutions in smarty are AWKWARD! *}
+      var msg = enabled ? '{ts escape="js" 1="<em>%1</em>"}%1 Disabled{/ts}' : '{ts escape="js" 1="<em>%1</em>"}%1 Enabled{/ts}'{literal};
+      return ts(msg, {1: fieldLabel});
+    }
+
     function refresh() {
       // Call native refresh method on ajax datatables
       if ($.fn.DataTable.fnIsDataTable($table[0]) && $table.dataTable().fnSettings().sAjaxSource) {
-        $table.unblock().dataTable().fnDraw();
+        $.each($.fn.dataTable.fnTables(), function() {
+          $(this).dataTable().fnSettings().sAjaxSource && $(this).unblock().dataTable().fnDraw();
+        });
       }
       // Otherwise refresh the content area using crmSnippet
       else {
         $row.closest('.crm-ajax-container, #crm-main-content-wrapper').crmSnippet().crmSnippet('refresh');
       }
-      {/literal} {* client-side variable substitutions in smarty are AWKWARD! *}
-      var msg = enabled ? '{ts escape="js" 1="<em>%1</em>"}%1 Disabled{/ts}' : '{ts escape="js" 1="<em>%1</em>"}%1 Enabled{/ts}'{literal};
-      CRM.alert('', ts(msg, {1: fieldLabel}), 'success');
     }
 
     function save() {
       $table = $row.closest('table');
       $table.block();
-      CRM.api(info.entity, 'setvalue', {id: info.id, field: 'is_active', value: enabled ? 0 : 1}, {success: refresh});
+      CRM.api3(info.entity, 'setvalue', {id: info.id, field: 'is_active', value: enabled ? 0 : 1}, {success: successMsg}).done(refresh);
       if (enabled) {
         $(this).dialog('close');
       }
@@ -86,8 +91,8 @@
 
     // Because this is an inline script it may get added to the document more than once, so remove handler before adding
     $('body')
-      .off('click', '.action-item.crm-enable-disable')
-      .on('click', '.action-item.crm-enable-disable', enableDisable);
+      .off('.crmEnableDisable')
+      .on('click.crmEnableDisable', '.action-item.crm-enable-disable', enableDisable);
   });
 </script>
 {/literal}