I've run into a problem using AJAX on two views on the same page using a recent build of views-7.x-3.x-dev. I've also tested Drupal 6, which doesn't seem to have the same problem.

Say we've defined views view1 and view2, both using AJAX (for pagers, exposed filters, or both), and we have a page calling

views_embed_view('view1', 'default');
views_embed_view('view2', 'default');

If we visit the page and update view1, it works fine (at first). If we proceed to update view2, it works just fine. However, if we try to update view1 after view2, it won't work. In fact, we'll find that it's actually requesting data for view2.

The problem has to do with the way the Javascript settings are indexed on the server side. The function template_preprocess_views_view() adds a settings array defined as follows:

    $settings = array(
      'views' => array(
        'ajax_path' => url('views/ajax'),
        'ajaxViews' => array(
          array(
            'view_name' => $view->name,
            'view_display_id' => $view->current_display,
            'view_args' => check_plain(implode('/', $view->args)),
            //Snipped several lines for brevity...
          ),
        ),
      ),
    );

The first time we load the page, all the settings are merged when drupal_add_js calls drupal_array_merge_deep and the data for view1 will be sent to Drupal.settings.views.ajaxViews.0 and the data for view2 will be sent to Drupal.settings.views.ajaxViews.1.

Now if we just go and update view1, everything is going to be fine. Pretty much the same thing happens except that no settings for view2 are sent to the browser. However, if we update view2, this time there's no data for view1, so the data for view2 ends up at Drupal.settings.views.ajaxViews.0, overwriting the settings for view1.

One fix is to change the way $settings['views']['ajaxViews'] is keyed in template_preprocess_views_view(). If we just key this array using a string determined by the view_dom_id, it works fine. (It's not enough to just use the view_dom_id, because drupal_array_merge_deep() will discard numerical keys for sequential ones).

Comments

serialjaywalker’s picture

Status: Active » Needs review
StatusFileSize
new527 bytes

Patch attached.

dawehner’s picture

Status: Needs review » Fixed

This makes absolute since, changed viewsDomId to views_dom_id and commited it to 7.x-3.x and 6.x-3.x. Thanks for providing this patch!

Status: Fixed » Closed (fixed)

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