2013-06-05 40 views
0

我使用JQuery動態追加GWT腳本,然後使用JQuery歷史跟蹤歷史記錄。JQuery和GWT歷史記錄支持混合起來

問題:我的GWT模塊生成History標記,因爲我的所有GWT模塊都是MVP模塊。並且onClick() s的MenuItem s正在動態加載GWT模塊,以及我正在使用JQuery爲MenuItems添加歷史記錄。但我的GWT模塊也使用History。現在的問題是,如果我反覆點擊加載GWT MVP modulesMenuItem,那麼我的瀏覽器正在被攻擊。

代碼:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
        <script src="/newclickmenu/jquery.history.js"></script> 

       // Contains code which creates menuItems using JSTL. In this menuItem there will be menuItem and *subMenuItem(onclick of menuItem there will be a slideDown which would show subMenuItem 


        <script type="text/javascript"> 
         jQuery(window).load(function() { 
          $('body').on('click', '#subMenuItem', function(event) { 
           // onClick of subMenuItem(which is anchor) then it add the History and will invoke the GWT mvp module. 
           // After doing the push then below `$.history.on('load change push', function(event, url, type) {` line is called. 
           $.history.push($(this).attr("href")); 
           event.preventDefault(); 
          }); 
          $.ajaxSetup({ cache: true }); 
          $.history.on('load change push', function(event, url, type) { 
           if (event.type == "load") { 
            if (url.split("!").length < 2) { 
             window.location = url; 
            } 
           } else { 
            // This code will add the GWT module dynamically. 
            if (typeof url !== undefined && url !== null && url !== "") { 
             // Remove the old GWT script and append the new script 
             var previousModule = $(".mainModule"); 
             if (previousModule.parent().length !== 0) { 
              $(previousModule).remove(); 
             } 
             var expectedUrl = "<script class='mainModule' src='/Masters/Masters.cache.js'/>"; 
             $('head').append($(expectedUrl)); 
            } 
           } 
         )}; 
          }).listen('hash'); 
        </script> 
+0

我建議你應該添加你的一些代碼。 – qben

+0

我已經添加了代碼,請檢查它。 –

回答

0

您是否嘗試過禁用GWT's歷史?

閱讀官方文檔here

To use GWT History support, you must first embed an iframe into your host HTML page. 
    <iframe src="javascript:''" id="__gwt_historyFrame" 
      style="width:0;height:0;border:0" /> 

嘗試將其刪除,重新編譯的模塊。

無論如何只有好奇心,爲什麼你使用jQuery注入GWT的腳本?我認爲以相反的方式做得更好。

您可以創建一個活動和地點設計,以一種非常簡單的方式控制您的歷史記錄,併爲您的jQuery腳本使用GWT的腳本注入器。

final JavaScriptObject idScript = $(window).prop("IDSCRIPT"); 
    if (idScript == null){ 
     ScriptInjector.fromUrl("js/jq/myJqueryScript.js").setCallback(
     new Callback<Void, Exception>() { 
      public void onFailure(Exception reason) { 
       AppConf.C("Trying to inject js/fb/myJqueryScript. Fail."); 
      } 
      public void onSuccess(Void result) { 
       executeMyStuff(...); 
      } 
     }).inject(); 
    } else { 
     executeMyStuff(...); 
    } 
+0

請檢查我的問題。我已經添加了代碼。 –

+0

那麼情況已經完全改變了。爲什麼每次都注入GWT模塊? 好吧,這個想法是加載GWT模塊一次,並且在它裏面你可以控制你的小部件和任何你想要的東西,只要你想要。 你是什麼意思被擊中?任何consolo錯誤?任何圖片?你能給我們更多的細節嗎? – apanizo

+0

是的,apanizo。我試圖讓我的模塊呈現更快。單擊menuItem它只是做派遣。所以,我雖然要削減這個Http請求。我幾乎減少了Http請求。此外,我正在使用緩存的Js,因此渲染時間會比正常情況更快。 問題明智:如果是谷歌瀏覽器,它會要求我殺死並重新加載頁面。 我在想,我一次調用JQuery歷史記錄和我的gwt模塊歷史更改事件。 –

0

我覺得你的設置是非常複雜的,我不知道你需要這在你的應用程序的原因,但無論如何:

1 - 當您添加GWT模塊無法卸載它。因此,儘管您刪除了腳本標記,但代碼已加載到內存中,並且所有事件gwt已添加到執行中。

2-如果您的gwt模塊更改歷史記錄令牌,您的jQuery代碼偵聽歷史事件將每次調用。

3-此設置可以用於加載的第一個gwt模塊,但第二個模塊會干擾第一個模塊,因爲兩者都將某些事件處理程序添加到同一事件。因此,每個加載的mvp gwt-module都在監聽歷史事件,鼠標事件等,並且您將得到一個不確定的行爲。

也許如果您對應用程序體系結構和要求有更多的解釋,我們可以找出最合適的設置。

+0

請查看解釋用於討論目的的簡化體系結構的鏈接。 http://stackoverflow.com/questions/16983265/how-to-load-gwt-modules-using-the-jquery –

+0

下面的鏈接顯示了我目前的項目架構和我的用戶面臨的問題。所以,請幫助我在實現目標方面做些什麼? http://stackoverflow.com/questions/16983265/how-to-load-gwt-modules-using-the-jquery 我已經取得了一個可能的進展,但由於你提到的問題,即..,* *代碼被加載到內存中**我已經被擊中了。我無法取得進展。請幫助我找到最合適的設置。 –