How does jQuery.sap.require work?

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

Does SAPUI5 load the libraries each time I call jQuery.sap.require("someLibrary")? For instance if I am calling the above statement in multiple modules in my application, is "someLibrary" loaded multiple times also?

回答1:

The lib is only loaded once. You can find this information in the SDK https://sapui5.hana.ondemand.com/sdk/#docs/guide/ModularizationConcept.html

Module Loading

As mentioned already, modules are loaded by calling function jQuery.sap.require with the name of a required module. The framework then checks whether the named module is loaded already. If so, the function simply returns. Otherwise it tries to load and execute the module synchronously. If any of these two steps fails, an exception is thrown and execution of the calling module thereby is disrupted.



回答2:

In case someone still considers to use jQuery.sap.require, be aware that it sends only sjax requests which should be avoided.

The use of jQuery.sap.require is synchronous and considered as "bad practice" because syncXHR is deprecated by the Web Hypertext Application Technology Working Group. [source]

Instead, the current best practice is to use sap.ui.define or .require for asynchronous module loading:

sap.ui.define([ // or .require   // modules to load ], function (/*modules available once loaded*/) {   // ... })

source: Asynchronify Your App

Same as jQuery.sap.require, sap.ui.require also loads module only once since both APIs call the same function named requireModule internally in which the module gets loaded depending on its state.


Sync XHRs will be deprecated not only by the web platform generally, but also by UI5 soon.


Update: The use of jQuery.sap.require is now deprecated. The same goes for jQuery.sap.declare.



回答3:

When you call this function with some library, it checks that given library is loaded or not using associative array. If the library is loaded, then it returns null. And if the library is not loaded, then it loads the library using sjax call and after a success of the sjax call, it sets the library name as key into associative array.



回答4:

The libraries are loaded once. This can be seen in the network tab in chrome developer tools.

Also check the documentation as pointed by cevou here:



转载请标明出处:How does jQuery.sap.require work?
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!