[英]SAPUI5 - oModel.create is not a function
我正在嘗試構建一個簡單的 SAPUI5 應用程序,它基本上列出了一個填充了員工數據(ID、姓名、地址)的表。
但是,我無法添加新員工,因為我總是遇到同樣的錯誤:
Uncaught TypeError: oModel.create is not a function at constructor.Save (EmpDetails.controller.js?eval:87)
你能幫我解決這個問題嗎? 我不明白為什么創建 function 不起作用,因為它連接到 model 並且應該可以正常工作(就像在初始化時填充表格的 GET 方法一樣)。 這是我的 controller 代碼:
sap.ui.controller("zemployee_crud.EmpDetails", { onInit: function() { var sServiceUrl = "proxy/http/<server>:<port>/sap/opu/odata/sap/ZEMPLOYEE_SRV"; var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true); var oJsonModel = new sap.ui.model.json.JSONModel(); oModel.read("/EmployeeSet",null,null,true,function(oData,response){ oJsonModel.setData(oData); }); sap.ui.getCore().setModel(oJsonModel); }, Save: function() { var oId = sap.ui.getCore().byId("Id").getValue(); var oName = sap.ui.getCore().byId("Name").getValue(); var oAddress = sap.ui.getCore().byId("Address").getValue(); var oEntry = {}; oEntry.Empid = oId; oEntry.Empname = oName; oEntry.Empadd = oAddress; var oModel = sap.ui.getCore().getModel(); oModel.create("/EmployeeSet", oEntry, null, function (response) { // success message // table reload }, function (Error) { //show error }); } });
您的 JSONModel 不支持create
。 您必須使用 ODataModel。
首先,您必須將 model 存儲在某處
var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);
this.getView().setModel(oModel);
然后稍后您可以訪問 model 並調用 create。
var oModel = this.getView().getModel();
oModel.create(...);
這是最簡單的方法。 我強烈建議了解該框架:
sap.ui.model.odata.ODataModel
已棄用。 使用sap.ui.model.odata.v2.ODataModel
。sap.ui.getCore().setModel()
)。 核心處於非常高/全球的水平。 大多數情況下,將其設置在應用程序/組件級別(在您的 manifest.json 或 Component.js 中)或 controller 級別就足夠了。我也強烈建議做官方演練。 完成需要 1-2 天,但之后你幾乎是專業人士,因為它回答了 95% 的初學者問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.