在drupal中执行ajax调用而不是bind()

时间:2012-09-17 19:13:45

标签: javascript jquery ajax drupal-7

我有一个绑定到窗口popstate事件的AJAX调用。它工作,除了我正在从查询字符串解析参数。问题是当页面加载时,ajax调用被绑定到窗口,所以我的参数在url执行时不会改变。我在想如果我向窗口添加一个监听器,如果我只能执行ajax调用而不是绑定它,它每次都会重新解析查询字符串。

例如,如果页面加载domain.com?x=1&y=2,那么我的调用发送x = 1和y = 2。但是当popstate触发并且变为domain.com?x=1&y=2&z=3时,它仍会发送x = 1和y = 2.

这是我的电话

var ajax_path = Drupal.settings.views.ajax_path;
$.each(Drupal.settings.views.ajaxViews, function (i, settings) {
    var view = '.facetapi-facetapi-links';
    element_settings = {
        url: ajax_path,
        submit: settings,
        setClick: false,
        event: 'popstate',
        selector: view,
        progress: { type: 'throbber' }
    };
    var viewData = {};
    $.extend(
        viewData,
        settings,
        Drupal.Views.parseQueryString(document.URL),
        Drupal.Views.parseViewArgs(document.URL, settings.view_base_path)
    );
    $.extend(viewData, Drupal.Views.parseViewArgs(document.URL, settings.view_base_path));
    element_settings.submit = viewData;
    var ajax = new Drupal.ajax(false, window, element_settings);
});

我在想的是做了这件事,但我不确定如何做。或者是否有可能以某种方式使用Drupal.ajax.prototype.beforeSend?

window.addEventListener("popstate", function(e) {
    //execute ajax here so it reparses document.url
}

1 个答案:

答案 0 :(得分:0)

我想出了这个解决方案,我正在使用自定义事件,以便我可以手动触发事件

window.addEventListener("popstate", function(e) {        
    $(window).unbind('custom');
    $.each(Drupal.settings.views.ajaxViews, function (i, settings) {
        element_settings = {
            url: Drupal.settings.views.ajax_path,
            submit: settings,
            setClick: false,
            event: 'custom',
            selector: '.facetapi-facetapi-links',
            progress: { type: 'throbber' }
        };

        var viewData = {};
        $.extend(
            viewData,
            settings,
            Drupal.Views.parseQueryString(document.URL),
            // Extract argument data from the URL.
            Drupal.Views.parseViewArgs(document.URL, settings.view_base_path)
        );

        $.extend(viewData, Drupal.Views.parseViewArgs(document.URL, settings.view_base_path));

        element_settings.submit = viewData;

        var ajax = new Drupal.ajax(false, window, element_settings);
        console.log(element_settings);
    });
    $(window).trigger('custom');
});