0

我正在使用 JQuery 动态附加 GWT 脚本,然后使用 JQuery 历史记录跟踪历史记录。

问题:我的 GWT 模块生成History令牌,因为我所有的 GWT 模块都是 MVP 模块。并且 onClick()s of MenuItems 动态加载 GWT 模块,同时我正在添加MenuItems使用 JQuery 的历史记录。但我的 GWT 模块也使用History. 现在的问题是,如果我反复单击MenuItemwhich GWT MVP modulesloading ,我的浏览器会被击中。

代码:

                    <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>
4

2 回答 2

0

您是否尝试禁用 GWT 的历史记录?

在此处阅读官方文档

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 的脚本?我认为以相反的方式做会更好。

您可以创建 Activity 和 Places 设计,以非常简单的方式控制您的历史记录,并为您的 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(...);
    }
于 2013-06-06T08:23:39.493 回答
0

我认为您的设置非常复杂,我不知道您在应用程序中需要它的原因,但无论如何:

1- 添加 gwt 模块时,您无法卸载它。因此,尽管您删除了脚本标签,但代码仍会加载到内存中,并且 gwt 添加的所有事件都会继续执行。

2-如果您的 gwt-module 更改历史令牌,则每次都会调用您的 jquery 代码侦听历史事件。

3- 此设置适用于加载的第一个 gwt 模块,但第二个模块会干扰第一个模块,因为两者都将某些事件处理程序添加到同一事物中。所以每个加载的 mvp gwt-module 都在监听历史事件、鼠标事件等,你会得到一个不确定的行为。

也许如果您对您的应用程序架构和要求进行更多解释,我们可以找出最合适的设置。

于 2013-06-07T06:33:01.113 回答