0

我有一个绑定到窗口 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
}
4

1 回答 1

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');
});
于 2012-10-01T23:57:23.443 回答