The SAPUI5 supports encapsulation of components with component. To encapsulate a component, the basic code for component is as follows:
Sap.ui.define ([ "sap/ui/core/uicomponent"], function (UIComponent) { "use strict"; Return Uicomponent.extend ("", { init:function () { //Call the Init function of the parent Uicomponent.proto Type.init.apply (this, arguments);});} );
Analyzing the code meaning of the component framework, referencing the uicomponent base space in the core, the component is written in Uicomponent.extend, which is extended.
We tried to encapsulate the previous application as a component, creating a new Component.js file with the following code:
Sap.ui.define ([ "Sap/ui/core/uicomponent", "Sap/ui/model/json/jsonmodel", " sap/ui/model/resource/ Resourcemodel "], function (UIComponent, Jsonmodel, Resourcemodel) { " use strict "; Return Uicomponent.extend ("Sap.ui.demo.wt.Component", { metadata: {rootview: "sap.ui.demo.wt.view.App"}, Init:function () { UIComponent.prototype.init.apply (this, arguments); var oData = { recipient: { name: "World" } }; var Omodel = new Jsonmodel (oData); This.setmodel (Omodel); var I18nmodel = new Resourcemodel ({ bundlename: "sap.ui.demo.wt.i18n.i18n" }); This.setmodel (I18nmodel, "i18n");});
We put the original Controller.js file in the initialization function, data model binding configuration and so on Component.js, the corresponding modification controller.js file:
Sap.ui.define ([ "Sap/ui/core/mvc/controller", "sap/m/messagetoast"], function (Controller, messagetoast) { "Use strict"; Return Controller.extend ("Sap.ui.demo.wt.controller.App", { onshowhello:function () { var obundle = This.getview (). Getmodel ("i18n"). Getresourcebundle (); var srecipient = This.getview (). Getmodel (). GetProperty ("/recipient/name"); var sMsg = Obundle.gettext ("Hellomsg", [srecipient]); Messagetoast.show (SMSG);});
In the Controller.js file, only the functions that need to be used in this project are kept, which makes the logic of each file in the project clearer.
In index.html, we can call component directly:
<script> Sap.ui.getCore (). Attachinit (function () { new Sap.ui.core.ComponentContainer ( name: " SAP.UI.DEMO.WT " }). Placeat (" content "); }); </script>
In SAP Fiori applications, each application has a configuration file, Manifest.json, which defines the project configuration information for some columns. The manifest file for this example is as follows:
{ "_version": "1.1.0", "Sap.app": {"_version": "1.1.0", "id": "SAP.UI.DEMO.WT",//define namespace "type": "Application" , "i18n": "I18n/i18n.properties", "title": "{{appTitle}}", "description": "{{appdescription}}", "Applicationversion": { "version": "1.0.0"}, "Ach": "Ca-ui5-doc" }, "Sap.ui": {"_version": "1.1.0", "Technology": "UI5", " Devicetypes ": { " desktop ": True, " tablet ": true, " phone ": true}," Supportedthemes ": [ " Sap_ Bluecrystal "] }, " Sap.ui5 ": {" _version ":" 1.1.0 "," Rootview ":" Sap.ui.demo.wt.view.App "," dependencies ": { "Minui5version": "1.30", "libs": {"sap.m": {}}, "models": {"i18n": {"type": " Sap.ui.model.resource.ResourceModel "," settings ": { " bundlename ":" Sap.ui.demo.wt.i18n.i18n "}}} }
As you can see, the Manifest.json file defines a series of basic information including the UI5 version, the data model, and so on. The configuration file will be continuously perfected during the development process.
HTML5 developing mobile Web Applications--sap UI5 (7)