In attempting an AJAX refresh on several views contained in separate blocks on a single page, I seem to be coming across a problem. This is running on D7.34
The idea is for the page to contain 4 blocks, each with a view sorting by random - then using a simple click() on the block title to activate a refresh of any given view allowing the user to view a new random item. I am using the common:
var selector = '.view-dom-id-' + dom_id;
jQuery(selector).triggerHandler('RefreshView');
to achieve this, which works perfectly. The trouble I am having is in increasing multiples of refreshes:
clicking on one view refresh does refresh the view, but it seems to create a counter, when a different view is refreshed, that second view refreshes twice. Then going back to the first, the view would refresh 3 times, and so on. It doesn't happen if clicking on the same view multiple times - in this case it only refreshes once as expected, it only occurs when switching between refreshing different views, however it continues to "count" these refreshes which then effect the number of times a different view is refreshed when the other refresh is triggered.
- I have confirmed that the selector is correct and obtains the unique dom_id for that view
- I have confirmed that the call jQuery(selector).triggerHandler('RefreshView'); only occurs once per click
- this occurs in Drupal.Behaviours, drupal_add_js, and straight JS added to a tpl file - they all "work" but with the same refresh issue
- I have used once() in the call as well with no perceived effect
It seems like when the triggerHandler triggers the refresh, the view itself is refreshing multiple times for reasons I can't seem to figure out. I have tried dozens of permutations now without any change in behaviour. I can't post of all the code from my attempts, but here is the last attempt which "works" but still refreshes multiple times - this is a straight JS addition to page.tpl, layed out for easier reading:
<script>
jQuery(document).ready(function()
{
jQuery('#block-views-random-cat-block h2').click(function()
{
jQuery.each(Drupal.views.instances, function(i, view)
{
if (view.settings.view_name == "random_cat")
{
var selector = '.view-dom-id-' + view.settings.view_dom_id;
jQuery(selector).triggerHandler('RefreshView');
}
});
});
jQuery('#block-views-random-dog-block h2').click(function()
{
jQuery.each(Drupal.views.instances, function(i, view)
{
if (view.settings.view_name == "random_dog")
{
var selector = '.view-dom-id-' + view.settings.view_dom_id;
jQuery(selector).triggerHandler('RefreshView');
}
});
});
});
</script>
If I limit the page to one view only, it seems to work fine - it's only in the fact that there are multiple views that I see this effect.