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: