简体   繁体   English

SAPUI5 - oModel.create 不是 function

[英]SAPUI5 - oModel.create is not a function

I'm trying to build a simple SAPUI5 application that basically lists a table filled with employee data (ID, name, address).我正在尝试构建一个简单的 SAPUI5 应用程序,它基本上列出了一个填充了员工数据(ID、姓名、地址)的表。 在此处输入图像描述

However, I cannot add new employee because I always get the same error:但是,我无法添加新员工,因为我总是遇到同样的错误:

Uncaught TypeError: oModel.create is not a function at constructor.Save (EmpDetails.controller.js?eval:87) Uncaught TypeError: oModel.create is not a function at constructor.Save (EmpDetails.controller.js?eval:87)

Could you please help me solve this?你能帮我解决这个问题吗? I don't understand why the create function does not work given that it is connected to the model and should work fine (just like the GET-method that fills the table on initialization).我不明白为什么创建 function 不起作用,因为它连接到 model 并且应该可以正常工作(就像在初始化时填充表格的 GET 方法一样)。 Here is my code for the controller:这是我的 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 }); } });

Your JSONModel does not support create .您的 JSONModel 不支持create You have to use an ODataModel.您必须使用 ODataModel。

First you have to store your model somewhere首先,您必须将 model 存储在某处

var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);
this.getView().setModel(oModel);

Then later you can access the model and call create.然后稍后您可以访问 model 并调用 create。

var oModel = this.getView().getModel();
oModel.create(...);

This is the most simple approach.这是最简单的方法。 I strongly suggest getting to know the framework:我强烈建议了解该框架:

  • sap.ui.model.odata.ODataModel is deprecated. sap.ui.model.odata.ODataModel已弃用。 Use sap.ui.model.odata.v2.ODataModel .使用sap.ui.model.odata.v2.ODataModel
  • Declare your model in your manifest.json.在 manifest.json 中声明您的 model。 Then all controllers can access it.然后所有控制器都可以访问它。
  • In most cases you don't need an extra JSONModel.在大多数情况下,您不需要额外的 JSONModel。 You can directly use the ODataModel and show it in your list您可以直接使用 ODataModel 并将其显示在您的列表中
  • Setting any model (JSON or OData) to the core ( sap.ui.getCore().setModel() ) is not necessary.无需将任何 model(JSON 或 OData)设置为核心( sap.ui.getCore().setModel() )。 The core is on a very high/global level.核心处于非常高/全球的水平。 Mostly it's enough to set it on an app/component level (either in your manifest.json or in the Component.js) or on a controller level.大多数情况下,将其设置在应用程序/组件级别(在您的 manifest.json 或 Component.js 中)或 controller 级别就足够了。

I also strongly suggest doing the official walkthrough .我也强烈建议做官方演练 It takes 1-2 days to complete but after that you are almost a pro since it answers 95% of all beginner questions.完成需要 1-2 天,但之后你几乎是专业人士,因为它回答了 95% 的初学者问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM