CommonJS modules can be used for node.js applications or in most modern browsers.
module.exports
. Every property attached to this object can be used later. Functions or variables, which are not attached as property to this object, are just visible inside the module.setBirthday
and getBirthday
, whereas the function normalizeBirthday
is just visible inside the module.
let birthday;
function normalizeBirthday() {
birthday.setHours(0);
birthday.setMinutes(0);
birthday.setSeconds(0);
}
module.exports.setBirthday = date => {
birthday = new Date(date);
normalizeBirthday();
}
module.exports.getBirthday = () => {
return birthday;
}
setBirthday
, the function getBirthday
returns this day in every invocation. When the process terminates, the cache is cleared.require
function. The require function returns the module.exports
-object. Assuming the above module is declared inside a file birthday.js, it can be imported from another module in the same folder like this:
const birthday = require('./birthday');
birthday.setBirthday('2002-05-08');
import
statement can be used instead. This statement allows the import of a property subset or the import of the whole module.setBirthday
😞
import { setBirthday } from './birthday'.
import birthday from './birthday';
sap.ui.define
function. If no module name is specified, the first argument of this function is an array with dependencies (other modules). The second argument is the so called factory function, which contains the logic of this module. The dependencies must be supplied in the correct order to this function. The following module uses the dependency sap/ui/model/odata/v2/ODataModel
to define an ODATA model object.
sap.ui.define(["sap/ui/model/odata/v2/ODataModel"], function(ODataModel){
"use strict";
return new ODataModel("/sap/opu/odata/sap/PP_MRP_COCKPIT_SRV");
});
sap.ui.define
function, but it's not recommended.
sap.ui.define("Quickstart/App/model", ["sap/ui/model/odata/v2/ODataModel"], function(ODataModel){
"use strict";
return new ODataModel("/sap/opu/odata/sap/PP_MRP_COCKPIT_SRV");
});
sap.ui.require
to directly access UI5 modules:
sap.ui.require([
"./model"
], function (ODataModel) {
"use strict";
console.log(ODataModel);
});
sap.ui.define([
"sap/ui/core/mvc/Controller",
"./model"
], function (Controller, ODataModel) {
"use strict";
return Controller.extend("Quickstart.App", {
onInit : function () {
this.getView().setModel(ODataModel);
},
onChange: function (oEvent) {
var bState = oEvent.getParameter("state");
this.byId("ready").setVisible(bState);
}
});
});
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
10 | |
7 | |
6 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |