是否真的需要使用“attachInit"来监听全局初始化事件?功能? [英] Is it really required to listen for global init event using "attachInit" function?

查看:35
本文介绍了是否真的需要使用“attachInit"来监听全局初始化事件?功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在很多文章中都读到过,监听全局 init 事件以便仅在事件被触发后才触发应用程序逻辑是一个很好的做法".但我尝试这样做,否则仍然没有问题发生.当我全部检查网络选项卡时,无论代码的位置如何,都会首先加载核心库.然后加载依赖库的代码.

我试图寻找我的答案,但没有得到明确的答案.我尝试使用我编写的示例代码来检查它.但没有成功.

<头><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta charset="UTF-8"><title>我的宠物</title><脚本ID=测试"src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"data-sap-ui-theme="sap_belize"data-sap-ui-libs="sap.m"><脚本>var oImage2 = new sap.m.Image({src: "img/cat_sad.jpg",装饰:假,Alt:悲伤的猫"});oImage2.placeAt("内容");警报(不同的脚本标签");<脚本>sap.ui.getCore().attachInit(function() {警报(inside_attachinit");var oImage1 = new sap.m.Image({src: "img/dog_sad.jpg",装饰:假,Alt:悲伤的狗"});oImage1.placeAt("内容");});警报(outside_attachinit");<body id="content" class="sapUiBody"></html>

我想知道,如果浏览器已经为我们相应地确定了请求的优先级,为什么我们会被告知要遵循这种良好做法?

解决方案

这不仅是一个很好的做法",而且是绝对必要的,以启用

  • 依序一一检索依赖库和其他模块.
  • 它使用

    • 文件以异步方式并行检索.
    • 没有冻结行为.

    依赖于同步 XHR 是面向未来的,并且会使应用程序显着变慢.随着 UI5 越来越好,并准备进一步改进,请继续遵循最佳实践,以便与 UI5 一起进化".

    I read it in many articles that, "it is a good practice to listen for global init event in order to trigger your application logic only after the event has been fired". But I tried to do otherwise still no problem occurs. When I check the network tab all, irrespective of the placement of the code, the core libraries are loaded first. Then the code dependent on libraries is loaded.

    I tried searching for my answer but couldn't get a clear answer. I tried to check it using a sample code I wrote. But no success.

    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="X-UA-Compatible" content="IE=Edge">
            <meta charset="UTF-8">
            <title>My Pets</title>
            <script id="test"
                src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
                data-sap-ui-theme="sap_belize"
                data-sap-ui-libs="sap.m">
            </script>
            <script>
                var oImage2 = new sap.m.Image({
                    src: "img/cat_sad.jpg",
                    decorative: false,
                    alt: "sad cat"
                });
                oImage2.placeAt("content");
                alert("different_script_tag");
            </script>
            <script>
                sap.ui.getCore().attachInit(function() {
                    alert("inside_attachinit");
                    var oImage1 = new sap.m.Image({
                        src: "img/dog_sad.jpg",
                        decorative: false,
                        alt: "sad dog"
                    });
                    oImage1.placeAt("content");
                });
                alert("outside_attachinit");
            </script>
        </head>
        <body id="content" class="sapUiBody">
        </body>
    </html>
    

    I wish to know, if browser already prioritizes the requests accordingly for us, why we are told to follow this good practice?

    解决方案

    It is not only "a good practice", it's absolutely necessary in order to enable asynchronous loading.

    Activating asynchronous loading of modules requires listening to the init event. Otherwise, the app will crash because you're trying to access Image from sap.m although that library hasn't been loaded yet:

    <script id="sap-ui-bootstrap" src="https://ui5.sap.com/resources/sap-ui-core.js"
      data-sap-ui-theme="sap_belize"
      data-sap-ui-libs="sap.ui.core, sap.m"
      data-sap-ui-async="true"
    ></script>
    <script>
      var oImage2 = new sap.m.Image(/*...*/); // Uncaught TypeError: Cannot read property 'Image' of undefined!
    </script>
    


    If you're wondering why asynchronous loading is important, compare these two scenarios:

    1. Without listening to init (no aync possible):

    • Dependent libraries and other modules are retrieved sequentially one by one.
    • It uses sync XHR which is deprecated.
    • While the app loads, browser often freezes.
    • Users usually need to wait longer until they see something.

    2. With async (listening to init required):

    • Files are retrieved in parallel asynchronously.
    • No freezing behavior.

    Depending on synchronous XHR is not future-proof and makes the app significantly slower. As UI5 is getting better and better and preparing for further improvements, please keep following best-practices in order to "evolve" together with UI5.

    这篇关于是否真的需要使用“attachInit"来监听全局初始化事件?功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆