diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-10-29 10:01:28 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-10-29 10:01:28 +0000 |
commit | 5371104a2d10889c532bb5d345fa6d71c0a897d1 (patch) | |
tree | bbef382cea55823195e0cb7469074b743913bc74 /misc/drupal.js | |
parent | 068febde425f4521d61f863ffaca76da65916449 (diff) | |
download | brdo-5371104a2d10889c532bb5d345fa6d71c0a897d1.tar.gz brdo-5371104a2d10889c532bb5d345fa6d71c0a897d1.tar.bz2 |
- Patch #316225 by sun et al: allow behaviors to detach from AHAH/AJAX.
Diffstat (limited to 'misc/drupal.js')
-rw-r--r-- | misc/drupal.js | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/misc/drupal.js b/misc/drupal.js index 4cb99bd99..157a20fea 100644 --- a/misc/drupal.js +++ b/misc/drupal.js @@ -12,10 +12,15 @@ Drupal.jsEnabled = document.getElementsByTagName && document.createElement && do * * Behaviors are event-triggered actions that attach to page elements, enhancing * default non-Javascript UIs. Behaviors are registered in the Drupal.behaviors - * object as follows: + * object using the method 'attach' and optionally also 'detach' as follows: * @code - * Drupal.behaviors.behaviorName = function () { - * ... + * Drupal.behaviors.behaviorName = { + * attach: function(context) { + * ... + * }, + * detach: function(context) { + * ... + * } * }; * @endcode * @@ -38,7 +43,38 @@ Drupal.attachBehaviors = function(context) { context = context || document; // Execute all of them. jQuery.each(Drupal.behaviors, function() { - this(context); + if (jQuery.isFunction(this.attach)) { + this.attach(context); + } + }); +}; + +/** + * Detach registered behaviors from a page element. + * + * Developers implementing AHAH/AJAX in their solutions should call this + * function before page content is about to be removed, feeding in an element + * to be processed, in order to allow special behaviors to detach from the + * content. + * + * Such implementations should look for the class name that was added in their + * corresponding Drupal.behaviors.behaviorName.attach implementation, i.e. + * behaviorName-processed, to ensure the behavior is detached only from + * previously processed elements. + * + * @param context + * An element to detach behaviors from. If none is given, the document element + * is used. + * + * @see Drupal.attachBehaviors + */ +Drupal.detachBehaviors = function(context) { + context = context || document; + // Execute all of them. + jQuery.each(Drupal.behaviors, function() { + if (jQuery.isFunction(this.detach)) { + this.detach(context); + } }); }; |