是否真的需要使用 "attachInit" 函数来监听全局初始化事件?
Is it really required to listen for global init event using "attachInit" function?
我在很多文章中都读到过,"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"。但我尝试这样做仍然没有问题发生。当我检查所有网络选项卡时,无论代码的位置如何,都会首先加载核心库。然后加载依赖库的代码。
我尝试搜索我的答案,但没有得到明确的答案。我尝试使用我编写的示例代码来检查它。但是没有成功。
<!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>
我想知道,如果浏览器已经为我们相应地确定了请求的优先级,为什么我们被告知要遵循这种良好做法?
不仅"a good practice",绝对需要才能启用asynchronous loading。
激活模块的异步加载需要监听init
事件。否则,应用程序将崩溃,因为您尝试从 sap.m
访问 Image
尽管该库尚未加载:
<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>
如果您想知道为什么异步加载很重要,请比较这两种情况:
1。不听 init
(不可能有 aync):
- 依赖库和其他模块依次一一检索。
- 它使用已弃用的 sync XHR。
- 应用加载时,浏览器经常死机。
- 用户通常需要等待更长的时间才能看到一些东西。
2。使用异步(需要收听 init
):
- 文件以异步方式并行检索。
- 没有冻结行为。
依赖于同步 XHR 而非 future-proof 并使应用程序显着变慢。 UI5越来越好,准备进一步完善,请继续关注best-practices,与UI5一起"evolve"
我在很多文章中都读到过,"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"。但我尝试这样做仍然没有问题发生。当我检查所有网络选项卡时,无论代码的位置如何,都会首先加载核心库。然后加载依赖库的代码。
我尝试搜索我的答案,但没有得到明确的答案。我尝试使用我编写的示例代码来检查它。但是没有成功。
<!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>
我想知道,如果浏览器已经为我们相应地确定了请求的优先级,为什么我们被告知要遵循这种良好做法?
不仅"a good practice",绝对需要才能启用asynchronous loading。
激活模块的异步加载需要监听init
事件。否则,应用程序将崩溃,因为您尝试从 sap.m
访问 Image
尽管该库尚未加载:
<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>
如果您想知道为什么异步加载很重要,请比较这两种情况:
1。不听 init
(不可能有 aync):
- 依赖库和其他模块依次一一检索。
- 它使用已弃用的 sync XHR。
- 应用加载时,浏览器经常死机。
- 用户通常需要等待更长的时间才能看到一些东西。
2。使用异步(需要收听 init
):
- 文件以异步方式并行检索。
- 没有冻结行为。
依赖于同步 XHR 而非 future-proof 并使应用程序显着变慢。 UI5越来越好,准备进一步完善,请继续关注best-practices,与UI5一起"evolve"