Compatibility with Drupal.attachBehaviors

markus_petrux - January 24, 2009 - 05:13
Project:Views Bulk Operations (VBO)
Version:6.x-1.x-dev
Component:User interface
Category:bug report
Priority:normal
Assigned:kratib
Status:closed
Description

When views form is loaded dynamically using AHAH/AJAX the Views Bulk Operations behavior is broken.

To fix this, vbo would have to implement its startup code using Drupal.behaviors rather than if (Drupal.jsEnabled) { jQuery(document).ready(function() { .... See misc/textarea.js for an example. Then, it may not need to invoke the startup code from ajaxViewResponse() as I believe this would be done already by Drupal.Views.Ajax.ajaxViewResponse() in views/js/ajax_views.js. If not, then you may probably invoke that from your ajaxViewResponse() implementation.

#1

kratib - January 24, 2009 - 09:59
Assigned to:Anonymous» kratib
Status:needs review» postponed (maintainer needs more info)

I was expecting this. But can you explain what is currently broken with AHAH? I've tried Views with Ajax on and things seemed to be working properly.

#2

markus_petrux - January 24, 2009 - 12:01

I came here due to a problem in editablefields module: #359435: Editablefields will not work if Views Bulk Operations is also active

Note that any module can implement AHAH/AJAX stuff in views that causes to rebuild part of the DOM in the document, and that part may affect the whole view, hence that AHAH/AJAX code would need a method to attach behaviors to the new DOM elements. Thid is explained better on top of misc/drupal.js in 6.9:

/**
* Attach all registered behaviors to a page element.
*
* 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:
* @code
*    Drupal.behaviors.behaviorName = function () {
*      ...
*    };
* @endcode
*
* Drupal.attachBehaviors is added below to the jQuery ready event and so
* runs on initial page load. Developers implementing AHAH/AJAX in their
* solutions should also call this function after new page content has been
* loaded, feeding in an element to be processed, in order to attach all
* behaviors to the new content.
*
* Behaviors should use a class in the form behaviorName-processed to ensure
* the behavior is attached only once to a given element. (Doing so enables
* the reprocessing of given elements, which may be needed on occasion despite
* the ability to limit behavior attachment to a particular element.)
*
* @param context
*   An element to attach behaviors to. If none is given, the document element
*   is used.
*/
Drupal.attachBehaviors = function(context) {
  context = context || document;
  if (Drupal.jsEnabled) {
    // Execute all of them.
    jQuery.each(Drupal.behaviors, function() {
      this(context);
    });
  }
};

If VBO doesn't provide a Drupal.behaviors implementation, then other modules have no method to attach your behaviors to the new DOM elements that they may have loaded, and that can include the whole view, or part of it. Hence other modules doing AHA/AJAX in views will brake VBO.

#3

markus_petrux - January 24, 2009 - 14:25

In #2 I forgot to mention that Drupal.vbo.ajaxViewResponse() would have to call Drupal.attachBehaviors(), maybe it could simply do as Drupal.Views.Ajax.ajaxViewResponse() in views/js/ajax_views.js. That way editablefields would have the oportunity to attach its own behaviors to the new DOM elements loaded by VBO's ajax feature.

#4

kratib - January 29, 2009 - 00:32
Status:postponed (maintainer needs more info)» active

I checked in a fix in the latest dev. Please pull from CVS (DRUPAL-6--1 branch) or wait 12 hours before Drupal.org refreshes the release. The nice thing is that by declaring Drupal.behaviors.vbo, I was able to eliminate the whole ajaxViewResponse() hack altogether!

#5

kratib - February 19, 2009 - 08:27
Status:active» fixed

Fixed unless anyone says otherwise.

#6

markus_petrux - February 19, 2009 - 08:34

Yep: scmizer confirmed it works here:

http://drupal.org/node/359435#comment-1231352

Thank you! :)

#7

System Message - March 5, 2009 - 08:40
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.