There is selector without context parameter in Drupal.behaviors.states function . So when some scripts calls Drupal.attachBehaviors(context), states behavior attaches to whole page and creates duplicate event handlers.

Example - using http://drupal.org/project/active_tags and Drupal states functionallity on the same page.

Code from states.js.

Drupal.behaviors.states = {
  attach: function (context, settings) {
    for (var selector in settings.states) {
      for (var state in settings.states[selector]) {
        new states.Dependent({
          element: $(selector),
          state: states.State.sanitize(state),
          dependees: settings.states[selector][state]
        });
      }
    }

    // Execute all postponed functions now.
    while (states.postponed.length) {
      (states.postponed.shift())();
    }
  }
};

Replacement of element: $(selector) with element: $(selector,context) solved this problem for me.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nod_’s picture

Version: 7.12 » 8.x-dev

So, this will land soon #1419968: Replace $('selector', domelement) with $(domelement).find('selector') in 8.x
and it will be backported, so let's just change it to $context.find(selector); to make it easier on us later.

nod_’s picture

Status: Active » Needs review
FileSize
603 bytes
623 bytes

Status: Needs review » Needs work

The last submitted patch, core-js-states-selector-1507954-2-D7.patch, failed testing.

nod_’s picture

Status: Needs work » Needs review

forgot how to name the thing so that bot ignores it.

nod_’s picture

Need review from someone other than me.

silinor’s picture

I tested patch on Drupal 7. When "Hierarchical Select" send function as context it didn`t work properly.

droplet’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs manual testing, +Needs backport to D7

silinor's post in #6 suggests this breaks hierarchical select, if this is intended for backport it'd be good to confirm there's no regression either way.

silinor’s picture

When hierarchical select called attachBehaviors it sended function as context. States.js is not worked correctly with function as context and attach behavior to whole page. For example required state after 2 selections with HS clone '*' and it`s become '***'
I found no other regressions. Only with hierarchical select. I think this issue will appear if some module will send function as context.

nod_’s picture

Status: Needs review » Reviewed & tested by the community

This looks like a bug in hierarchical select. It's context variable is a function and is used as a jQuery element everywhere. This is unrelated to this patch. In the case of hierarchical select the context should be cached.

Also it's sending wrong data to attachbehaviors, it's a raw DOM element that should be passed to it, neither a function nor a jQuery object.

catch’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

OK that makes sense. Committed/pushed to 8.x, will probably need a bug report for hierarchical select before it can be fixed in 7.x though I think.

boombatower’s picture

Status: Patch (to be ported) » Needs review
FileSize
939 bytes
nod_’s picture

Status: Needs review » Reviewed & tested by the community

Filled the patch over in the HS queue #1655100: Wong data sent to Drupal.attachBehaviors

Wim Leers’s picture

Thanks for the bug report! I'll commit the fix today and issue a new release.

webchick’s picture

Doesn't look like that was actually done?

David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed

I'm not sure the Hierarchical Select issue was fixed yet (looks like probably not), but the module only has an alpha release so I don't think we have to wait forever for it. Also, if I understand the issue right, it's just that the bug fix doesn't work when Hierarchical Select is being used, not that anything else horrible breaks.

So... committed to 7.x. Thanks! http://drupalcode.org/project/drupal.git/commit/349ea19

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