Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
Hello Everyone,

 

我今天第一天开博,UI5也是新手,希望大家一起学习。

 

Model 就是我们的数据存储站,可以是JSon Model, OData Model 等,从Odata 取到的数据存在model,然后可能需要做Filter, Merge, Calculate,group 等都在Model中,相当于ABAP里的内表,WEB DYNPRO里的 Context Node.

那话到正题,我列以下三个问题,以下用代码一个一个解释:

1,怎么创建model


2.怎么使用model


3.怎么操作model


 

 

 

 

创建Model is easy, 以下code是在component 上创建一个global model,每个view 都能使用:
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/core/routing/History"
], function(Controller, History) {
"use strict";

return Controller.extend("sap.xx.BaseController", {

setModel: function(oModel, sName) {
return this.getOwnerComponent().setModel(oModel, sName);
}

}

 

------------------------------****----------------- 创建Json model


sap.ui.define([
"sap/support/idod/controller/BaseController",
"sap/ui/model/json/JSONModel",

{

this._oSalesOrderModel = new JSONModel({
bShowBusy: true
Quantity: "0",
order: "0",
Customer: "0"

});

this.setModel(this._oSalesOrderModel, "SalesOrder");

}

* 给Model 字段赋值
this.getModel("SalesOrder").setProperty("/Quantity", iQty);

 

 

***如何读Odata 并存储到Json model 示例:


getData: function() {

this.getModel().read("/SalesOrder", { "this is odata entityset name
filters: [new sap.ui.model.Filter("Dummy", "EQ", "Counts")],
success: function(oData) {
this._assginUpdteDelay(oData.results);
this.getModel("oSalesOrder").setProperty("/bShowBusy", false);
}.bind(this),

error: function(response) {
this.getModel("oSalesOrder).setProperty("/bShowBusy", false);
}.bind(this)
});
},

_assginUpdteDelay: function(oArray) {

this.getModel("SpcBcI7").setProperty("/DelayedInc", oArray.slice(3));
var i = 0;
while (i < Math.min(oArray.length, 3)) {
if (oArray[i].Filter === "bc") {
this.getModel("oUpdateDelay").setProperty("/BC", oArray[i].Failedlines);
} else if (oArray[i].Filter === "spc") {
this.getModel("oUpdateDelay").setProperty("/SPC", oArray[i].Failedlines);
} else if (oArray[i].Filter === "i7") {
this.getModel("oUpdateDelay").setProperty("/I7", oArray[i].Failedlines);
}
i++;
}
}

});

 

 

 

 

** 以下示范Model  Binding View:


this.setModel(new JSONModel({
"DelayedInc": []
}), "YBB");

* 定义了一个Model A,里面包含一个数组
this.setModel(new JSONModel({
"DelayedInc": [],
selectedLab: "(BC->I7)",
colorChar: "",
status_s: "Status(BC)",
status_t: "Status(LP)",
status_p: "Status(PB)",
switcher: true,
pointer: ""
}), "Updatedelay");

*定义了一个Model B,里面包含了数组和字段
AllDelayedInc: [],

this.AllDelayedInc = this.getModel("YBB").getData().DelayedInc;

*将Model B的数据 放到数组里


 
this.getModel("Updatedelay").setProperty("/status_s", "Status USA");

*将Model里的字段赋值


//Model Binding View Element

<columns>
<Column minScreenWidth="Tablet" demandPopin="true" width="15em">
<Text text="{Updatedelay>/pointer}"/>
</Column>
<Column minScreenWidth="Tablet" demandPopin="true" width="10%">
<Text text="{Updatedelay>/status_s}"/>
</Column>

*使用Model里的字段  model>/fieldname