How does jQuery.sap.require work?

2020-03-31 02:15发布

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?

标签: sap sapui5
4条回答
Lonely孤独者°
2楼-- · 2020-03-31 02:33

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.

查看更多
可以哭但决不认输i
3楼-- · 2020-03-31 02:39

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:

// Enabling asynchronous module loading in index.html (available since 1.58.2)
data-sap-ui-async="true" // replaces sap-ui-preload and sap-ui-xx-async
sap.ui.define([ // or .require
  // modules to load
], function (/*modules available once loaded*/) {
  // ...
});

Asynchronification of module dependency
source: Asynchronify Your App by Arnd vom Hofe

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.

Deprecation of Sync XHR
source: UI5 Evolution by Peter Müßig


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

查看更多
祖国的老花朵
4楼-- · 2020-03-31 02:44

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.

查看更多
对你真心纯属浪费
5楼-- · 2020-03-31 02:45

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:

查看更多
登录 后发表回答