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

i042416發表於2018-05-11

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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2154322/,如需轉載,請註明出處,否則將追究法律責任。

相關文章