All of the jquery calls in this function should use the 'context' variable as the second parameter to restrict the scope of their effect. Without this, everytime Drupal.attachBehaviors is called for an AHA or other AJAX style request, the JS adds additional 'Show More' links into the blocks that utilize them.
Sorry for not making a patch, but since the file is very small I'm simply posting my example of how to correct the issue. This is the contents of my apachesolr.js and it is currently working well for me.
Drupal.behaviors.apachesolr = function(context) {
$('.apachesolr-hidden-facet', context).hide();
$('<a href="#" class="apachesolr-showhide"></a>', context).text(Drupal.t('Show more')).click(function() {
if ($(this, context).parent().find('.apachesolr-hidden-facet:visible').length == 0) {
$(this, context).parent().find('.apachesolr-hidden-facet').show();
$(this, context).text(Drupal.t('Show fewer'));
}
else {
$(this, context).parent().find('.apachesolr-hidden-facet').hide();
$(this, context).text(Drupal.t('Show more'));
}
return false;
}).appendTo($('.block-apachesolr_search:has(.apachesolr-hidden-facet), .block-apachesolr:has(.apachesolr-hidden-facet)', context));
}
Comments
Comment #1
Scott Reynolds commentedActually, context really can't be relied on: http://lists.drupal.org/pipermail/development/2009-May/033059.html
So instead we should try to use classes to tell if its been processed or not. That is the best practice.
Comment #2
justindodge commentedInteresting link.
Well, so long as it's done in someway other than blindly reattaching to the whole document every time the issue would be solved for me. :)
Comment #3
pwolanin commentedDrupal core uses the class approach:
Comment #5
pwolanin commentedDiscussed w/ ksenzee and conclude the original approach is good enough and unlikely to be affected by the edge cases of using context.
Also, Earl supports using context: http://lists.drupal.org/pipermail/development/2009-May/033057.html
Comment #6
pwolanin commentedAfter further review - we really just need context in a couple places.
Comment #7
pwolanin commentedcommitting to 6.x-1.x
Comment #8
robertdouglass commentedThis patch takes the two context instances from #6 into consideration. Since the 6.2 file has more code I'm going to ask for a review of this issues from the jQuery gods - it may be that more context passing is needed.
Comment #9
claudiu.cristeaHere's a patch against DRUPAL-5--2.
Comment #10
claudiu.cristeaCommitted to DRUPAL-5--2 in #326224.
Switched back ti DRUPAL-6--2.
Comment #11
robertdouglass commented#666936 by pwolanin, robertDouglass, claudiu.cristea | justindodge: Fixed apachesolr.js - Drupal.behaviors.apachesolr does not respect context.
Fixed all around.