sap.ui.require in SAP UI5 and require in nodejs
2018-04-05 00:00
543 查看
UI5
例如我需要在controller的onShowHello里通过MessageToast弹一个消息显示在UI上,![](https://upload-images.jianshu.io/upload_images/2085791-e4a68a49a014423e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
我需要先定义我自己的controller,该controller extend自UI5标准的controller module,路径为sap/ui/core/mvc/Controller, 而为了调用MessageToast的show方法,也必须先拿到MessageToast的实例。所以UI5里使用sap.ui.define去加载这两个依赖的module,加载完成后,会调用应用开发人员传入的回调函数,加载的两个module的内容作为两个输入参数传入该回调函数。
![](https://upload-images.jianshu.io/upload_images/2085791-f248bed5907da81a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
UI5所有的module由jquery.sap.global.js中的Module统一管理。
![](https://upload-images.jianshu.io/upload_images/2085791-507c8262ef1b4b02.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/2085791-fd20f54c579d9bc9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
更多细节请查看我的SAP前同事Wu Ji的博客SAPUI5 walkthrough step 6 – modules, dive in – how does modules work?
当然也存在sap.ui.require这种简洁用法。一个UI5框架的使用例子:
![](https://upload-images.jianshu.io/upload_images/2085791-d96214d72061acfc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
nodejs
逻辑和UI5类似,假设我想使用queryString这个module,只需要用nodejs标准的require函数将其引入:![](https://upload-images.jianshu.io/upload_images/2085791-c28774675a2cd51e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后就可在应用代码里使用该module通过module.exports暴露出的方法:
![](https://upload-images.jianshu.io/upload_images/2085791-773e1a7cd6cfad07.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
require调用internal/module.js里的self.require:
![](https://upload-images.jianshu.io/upload_images/2085791-4a46d511fb6a9edd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
然后是Module._load:
![](https://upload-images.jianshu.io/upload_images/2085791-0638174df515c65a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
nodejs也有类似UI5的module统一管理器:Module._cache. 每次加载module时,首先从这个管理器里查看是否该module已经加载。只有当module在管理器里不存在时才会真正加载,new一个Module对象,维护好属性后存入module管理器。
![](https://upload-images.jianshu.io/upload_images/2085791-fa37209ba4539124.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
加载好的module就是一系列function的集合:
![](https://upload-images.jianshu.io/upload_images/2085791-f5136098f16c7b22.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
要获取更多Jerry的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:
![](https://upload-images.jianshu.io/upload_images/2085791-2486f3bd80b6ca71.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2085791-459dc9b052f3c62d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
相关文章推荐
- sap.ui.require in SAP UI5 and require in nodejs
- How to disable 'withcredentials' in HTTP header with node.js and Request package?
- [Node.js] Testing ES6 Promises in Node.js using Mocha and Chai
- Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br
- When to use next() and return next() in Node.js
- Where Does Node.js And Require() Look For Modules?
- 基于Nodejs的高并发实时消息转发系统 message pusher and written in nodejs based on socket.io and express
- 京JS 2013 - A two-day conference in Beijing for the JavaScript and Node.js community
- node-webkit中的requirejs报错问题:path must be a string error in Require.js
- 【Coffee】Nodejs and CoffeeScript installation in windows
- 用node.js实现require.js
- nw.js node-webkit系列(7)Native UI API Menu的使用
- nodejs error getaddrinfo ENOTFOUND in dns.js
- Reading and Writing data on application server in SAP
- Save and render a webpage with phantomjs and node.js
- [Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js
- Starting out with Node.js and AngularJS
- How to install Node.js and NPM on Windows
- Using ASP.NET Calendar Control and Yahoo.UI.Calendar in ASP.NET
- Part 14 ng hide and ng show in AngularJS