The ajaxViewResponse function is responsible for refreshing the view display.

After it does this, it calls Drupal.attachBehaviors() to attach behaviors to the new HTML fragment that has just been injected into the page.

However, instead of sending this new HTML to attachBehaviors(), it sends the old HTML that is swapped out.

(This bug stems from incorrect use of jQuery's replaceWith(). That method is quite unintuitive: the documentation explains that replaceWith() returns the "the JQuery element that was just replaced, which has been removed from the DOM".)

The patch does two things:

  1. It fixes that bug.
  2. It makes the page display any Drupal messages (response.messages). This is completely unrelated to the bug described here, but I can't provide it in a separate patch because these two patches will collide. This feature, of displaying the messages, could prevent in the future bugs such a this.
CommentFileSizeAuthor
attachbehaviors.diff1.18 KBmooffie

Comments

mooffie’s picture

Two notes:

  1. The reason View's behaviors are successfully attached to the new HTML in spite of the bug is because Drupal.behaviors.ViewsAjaxView doesn't utilize the context passed to it (it searches the views in the whole document).
  2. To attachBehaviors() I send $view.parent(), and not just $view, because the hypothetical pattern $('.view', context) doesn't match when context itself is the desired element.
merlinofchaos’s picture

Status: Needs review » Fixed

Well all right then. Looks good to me. Committed!

Anonymous’s picture

Status: Fixed » Closed (fixed)

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