From 3b2d24af0b8ca83415310e2b328cc60fa830837b Mon Sep 17 00:00:00 2001 From: Angie Byron Date: Tue, 3 Nov 2009 05:34:37 +0000 Subject: #561726 by effulgentsia, TwoD, and sun: Make ajax.js and tabledrag.js implement Drupal.detachBehaviors(). --- misc/ajax.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'misc/ajax.js') diff --git a/misc/ajax.js b/misc/ajax.js index 79217dfd6..0daa9bc4e 100644 --- a/misc/ajax.js +++ b/misc/ajax.js @@ -120,6 +120,9 @@ Drupal.ajax = function (base, element, element_settings) { var options = { url: ajax.url, data: ajax.button, + beforeSerialize: function (element_settings, options) { + return ajax.beforeSerialize(element_settings, options); + }, beforeSubmit: function (form_values, element_settings, options) { return ajax.beforeSubmit(form_values, element_settings, options); }, @@ -176,6 +179,18 @@ Drupal.ajax = function (base, element, element_settings) { } }; +/** + * Handler for the form serialization. + * + * Runs before the beforeSubmit() handler (see below), and unlike that one, runs + * before field data is collected. + */ +Drupal.ajax.prototype.beforeSerialize = function (element, options) { + // Allow detaching behaviors to update field values before collecting them. + var settings = this.settings || Drupal.settings; + Drupal.detachBehaviors(this.form, settings, 'serialize'); +}; + /** * Handler for the form redirection submission. */ @@ -230,6 +245,13 @@ Drupal.ajax.prototype.success = function (response, status) { } } + // Reattach behaviors that were detached in beforeSerialize(). The + // attachBehaviors() called on the new content from processing the response + // commands is not sufficient, because behaviors from the entire form need + // to be reattached. + var settings = this.settings || Drupal.settings; + Drupal.attachBehaviors(this.form, settings); + Drupal.unfreezeHeight(); // Remove any response-specific settings so they don't get used on the next @@ -280,6 +302,9 @@ Drupal.ajax.prototype.error = function (response, uri) { $(this.wrapper).show(); // Re-enable the element. $(this.element).removeClass('progress-disabled').attr('disabled', false); + // Reattach behaviors that were detached in beforeSerialize(). + var settings = response.settings || this.settings || Drupal.settings; + Drupal.attachBehaviors(this.form, settings); }; /** @@ -300,6 +325,17 @@ Drupal.ajax.prototype.commands = { // Safari with long string lengths. http://dev.jquery.com/ticket/3178 var new_content = $('
').html(response.data); + // If removing content from the wrapper, detach behaviors first. + switch (method) { + case 'html': + case 'replaceWith': + case 'replaceAll': + case 'empty': + case 'remove': + var settings = response.settings || ajax.settings || Drupal.settings; + Drupal.detachBehaviors(wrapper, settings); + } + // Add the new content to the page. wrapper[method](new_content); @@ -333,6 +369,8 @@ Drupal.ajax.prototype.commands = { * Command to remove a chunk from the page. */ remove: function (ajax, response, status) { + var settings = response.settings || ajax.settings || Drupal.settings; + Drupal.detachBehaviors($(response.selector), settings); $(response.selector).remove(); }, -- cgit v1.2.3