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.
Comment | File | Size | Author |
---|---|---|---|
#12 | core-js-states-selector-1507954-12.patch | 939 bytes | boombatower |
#2 | core-js-states-selector-1507954-2.patch | 623 bytes | nod_ |
#2 | core-js-states-selector-1507954-2-D7.patch | 603 bytes | nod_ |
Comments
Comment #1
nod_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.Comment #2
nod_Comment #4
nod_forgot how to name the thing so that bot ignores it.
Comment #5
nod_Need review from someone other than me.
Comment #6
silinor CreditAttribution: silinor commentedI tested patch on Drupal 7. When "Hierarchical Select" send function as context it didn`t work properly.
Comment #7
droplet CreditAttribution: droplet commentedComment #8
catchsilinor'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.
Comment #9
silinor CreditAttribution: silinor commentedWhen 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.
Comment #10
nod_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.
Comment #11
catchOK 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.
Comment #12
boombatower CreditAttribution: boombatower commentedComment #13
nod_Filled the patch over in the HS queue #1655100: Wong data sent to Drupal.attachBehaviors
Comment #14
Wim LeersThanks for the bug report! I'll commit the fix today and issue a new release.
Comment #15
webchickDoesn't look like that was actually done?
Comment #16
David_Rothstein CreditAttribution: David_Rothstein commentedI'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