As far as I understand, in many cases sap.ui.require
and sap.ui.define
are interchangeable. However, there is a convention:
Use
sap.ui.define
for controllers and all other JavaScript modules to define a global namespace. With the namespace, the object can be addressed throughout the application.Use
sap.ui.require
for asynchronously loading dependencies but without declaring a namespace, for example code that just needs to be executed, but does not need to be called from other code.
I read this statement several times, but I'm not sure I got it completely.
For instance, when I write an UI5-controller for the specific UI5-view, then what should I use? And when I write a BaseController.js
, which the rest of the controllers will be based on, then should I chose sap.ui.define
or sap.ui.require
?
According to the SAPUI5 team, sap.ui.define
is similar to the static import
and export
statements in ES6 modules, while sap.ui.require
is more like the dynamic import()
statement.
Basically all UI5 module definitions (incl. view controllers' code) should start with sap.ui.define
on the top level, while sap.ui.require
should be used if in the middle of the code I want to import some other UI5 modules.
For more details on the subject: Differences between sap.ui.require
and sap.ui.define
.