sap.ui.require in SAP UI5 and require in nodejs

UI5

例如我需要在controller的onShowHello里通过MessageToast弹一个消息显示在UI上,
sap.ui.require in SAP UI5 and require in nodejs

我需要先定义我自己的controller,该controller extend自UI5标准的controller module,路径为sap/ui/core/mvc/Controller, 而为了调用MessageToast的show方法,也必须先拿到MessageToast的实例。所以UI5里使用sap.ui.define去加载这两个依赖的module,加载完成后,会调用应用开发人员传入的回调函数,加载的两个module的内容作为两个输入参数传入该回调函数。

sap.ui.require in SAP UI5 and require in nodejs

UI5所有的module由jquery.sap.global.js中的Module统一管理。

sap.ui.require in SAP UI5 and require in nodejs

sap.ui.require in SAP UI5 and require in nodejs

更多细节请查看我的SAP前同事Wu Ji的博客SAPUI5 walkthrough step 6 – modules, dive in – how does modules work?

当然也存在sap.ui.require这种简洁用法。一个UI5框架的使用例子:

sap.ui.require in SAP UI5 and require in nodejs

nodejs

逻辑和UI5类似,假设我想使用queryString这个module,只需要用nodejs标准的require函数将其引入:

sap.ui.require in SAP UI5 and require in nodejs

然后就可在应用代码里使用该module通过module.exports暴露出的方法:

sap.ui.require in SAP UI5 and require in nodejs

require调用internal/module.js里的self.require:

sap.ui.require in SAP UI5 and require in nodejs

然后是Module._load:

sap.ui.require in SAP UI5 and require in nodejs

nodejs也有类似UI5的module统一管理器:Module._cache. 每次加载module时,首先从这个管理器里查看是否该module已经加载。只有当module在管理器里不存在时才会真正加载,new一个Module对象,维护好属性后存入module管理器。

sap.ui.require in SAP UI5 and require in nodejs

加载好的module就是一系列function的集合:

sap.ui.require in SAP UI5 and require in nodejs
要获取更多Jerry的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:

sap.ui.require in SAP UI5 and require in nodejs

sap.ui.require in SAP UI5 and require in nodejs

猜你喜欢

转载自blog.51cto.com/5899363/2107839