As the title says: the Drupal.behaviors.AJAX.attach() method in misc/ajax.js doesn't properly use the context parameter when creating jQuery objects. It is my understanding, that the context should always be used in those cases, and I don't really know why it isn't there.

Since the JS code checks for the ajax-processed class, this will usually not cause any errors, I think – but I currently have a use case where I want to attach behaviors to DOM nodes that aren't currently inside the normal document tree – and thus aren't found if context is ignored.

So, I wanted to know whether there's an actual reason to not use context there. And, if there isn't, it would of course be great if we could fix this.

Comments

drunken monkey created an issue. See original summary.

drunken monkey’s picture

Status: Active » Needs review
FileSize
1.79 KB

Here is a patch that fixes this for me.

drunken monkey’s picture

Version: 7.x-dev » 8.2.x-dev
FileSize
1.26 KB

Just took a look and it seems Drupal 8 has the same code. I'd guess that means it is on purpose, but just in case, here is also a patch for D8.
And, in any case, an explanation/rationale for this would be nice. (I guess it would even make sense to add a comment explaining this.)

droplet’s picture

Category: Bug report » Task
Status: Needs review » Needs work
Issue tags: +JavaScript

Umm. I think Ajax was designed as no context for above usages. And it's expected you passing `context` instead:
http://cgit.drupalcode.org/drupal/tree/core/misc/ajax.js#n200

Put it back to Needs work, in CORE we preferred chaining method: $(context).find(target).

drunken monkey’s picture

Status: Needs review » Needs work

The last submitted patch, 5: 2711821-5--ajax_behavior_attach_context--d7.patch, failed testing.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.