2012-09-17 71 views
0

我有一個綁定到窗口popstate事件的AJAX調用。它的工作原理,除了我解析查詢字符串的參數。問題是當頁面加載時,ajax調用被綁定到了窗口,所以當url的時候,我的參數不會改變。我想如果我添加一個監聽器到窗口,如果我可以執行ajax調用而不是綁定它,它會每次都重新解析查詢字符串。在drupal中執行ajax調用而不是綁定()

例如,如果頁面加載domain.com?x =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 
} 

回答

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'); 
});