Model通俗理解就是数据模型,在C层通过一些处理后把数据存储在这里,在V层去使用,扮演MVC结构中的M
Model类型分为JSONModel、ODataModel、ResourceModel、XMLModel
一、JSONModel
在JS中定义局部的Model,只可以在当前页面去使用,API:"sap/ui/model/json/JSONModel"
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/json/JSONModel",
"sap/ui/core/UIComponent"
], function(Controller, JSONModel, UIComponent) {
"use strict";
return Controller.extend("WALKTHROUGHZSAPUI5_WALKTHROUGH.controller.Detail", {
onInit: function() {
var oJSONModel = new JSONModel({
Name: "test"
});
var oView = this.getView();
oView.setModel(oJSONModel, "ZTDEMO");
}
});
});
在manifest中定义的Model是对整个项目来说是全局的,可以在view中直接引用
view:
<mvc:View controllerName="WALKTHROUGHZSAPUI5_WALKTHROUGH.controller.InvoiceList" xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc">
<List id="invoiceList" headerText="{i18n>invoiceListTitle}" class="sapUiResponsiveMargin" width="auto"
items="{ path : 'invoice>/Invoices',
sorter : {
path : 'ShipperName',
group : true
}
}">
<headerToolbar>
<Toolbar>
<Title text="{i18n>invoiceListTitle}"/>
<ToolbarSpacer/>
<SearchField width="50%" search="onFilterInvoices"/>
</Toolbar>
</headerToolbar>
<items>
<ObjectListItem title="{invoice>Quantity} x {invoice>ProductName}"
number="{ parts: [{path: 'invoice>ExtendedPrice'}, {path: 'view>/currency'}], type: 'sap.ui.model.type.Currency', formatOptions: { showMeasure: false }}"
numberUnit="{view>/currency}" numberState="{= ${invoice>ExtendedPrice} > 50 ? 'Error' : 'Success' }"
type="Navigation"
press="onPress">
<firstStatus>
<ObjectStatus text="{ path: 'invoice>Status', formatter: '.formatter.statusText' }"/>
</firstStatus>
</ObjectListItem>
</items>
</List>
</mvc:View>
二、ODataModel
var oModel = this.getOwnerComponent().getModel("ZTEST_DEMO");
var that = this;
oModel.read("/ETPERSONSet", {
success: function(oData, response) {
var gridModel = new JSONModel();
// sap.ui.getCore().navmodel = that.NavModel;
// level1JSONModel.setData(that.NavModel.getData());
gridModel.setData(oData.results);
that.byId("ztestdemo").setModel(gridModel);
that.getView().setModel(gridModel, "tedm");
},
error: function(oError) {
that.showErrorMessage(oError);
}
});
三、ResourceModel
// read msg from i18n model
var oBundle = this.getView().getModel("i18n").getResourceBundle();
var sRecipient = this.getView().getModel().getProperty("/recipient/name");
var sMsg = oBundle.getText("helloMsg", [sRecipient]);
// show message
MessageToast.show(sMsg);