如何在新实例化期间为实体创建 odata 模型 [英] How to create a odata model for an entity during new instantiation

查看:29
本文介绍了如何在新实例化期间为实体创建 odata 模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 CRUD 中单击新按钮时,如何基于实体类型创建 JSON 模型.

In CRUD when new button is clicked, how to create a JSON model, based of a entity type.

这个基于空实体的 JSON 模型可以从元数据实体类型派生并分配给视图.

This empty entity based JSON model can be derived from the metadata entity type and assigned to a view.

我尝试使用 Model.onOnMetaDataLoaded 来获取实体类型并使用具有初始值的默认 JSON 模型.但是我无法从元数据中获取实体类型,尽管我可以看到对象中的实体类型.

I tried to use Model.onOnMetaDataLoaded to the get the entity type and use that the default JSON Model with initial values. But I couldn't get the entity type from the metadata though I can see the entity type in the object.

var oModel = new sap.ui.model.odata.v2.ODataModel(<ServiceURL>);

oModel.attachMetadataLoaded(null, function(){
   var oMetadata = oModel.getServiceMetadata();
   console.log(oMetadata);
   var metaModel = new sap.ui.model.odata.ODataMetaModel(oMetadata);
   metaModel.getEntityType('XXX') // DOES NOT GIVE VALUE 
},null);

metaModel.getEntityType('XXX')//不给值

当我们创建新的实体类型时,我们是否需要基于实体类型来建立我们的 JSON 模型.我假设这将有助于验证数据类型而不是手动进行.

And do we need to base our JSON model based off of the entity type when creating new. I am assuming this would be of help in validating data type instead of doing manually.

推荐答案

既然你问了,不,我不这样做...我使用 oData 模型的功能来跟踪更改.任何支持绑定上下文的元素都可以这样工作,例如对话框、视图或简单表单.

Since you asked, no I don't do it this way... I use the oData model's capabilities to track changes. Any element that supports a binding context can work like this, such as a dialog, a view or a simple form.

以最简单的方式,它看起来像:

In its simplest way, it would look like:

myView.setBindingContext(this.getModel().createEntry("/MyEntitySet"));

如果你想要更多的控制,比如指定成功和错误处理程序(你可能会),就像:

If you want more control, like specify the success- and error handler (and you probably will), it's something like:

myView.setBindingContext(this.getModel().createEntry("/MyEntitySet", {
  changeSetId: 'myChanges', 
  properties: {
    myField: 'DefaultValue',
    myDate: new Date()
  },
  success: _ =>  myView.setBusy(false),
  error: _ => myView.setBusy(false)
}));

关于您是否需要自己指定字段的问题:不,您不需要,您只需将实体中的任何值直接绑定到一个字段.如果用户填写它们,它们将出现在创建时使用的对象中.您可以随时使用

Regarding your question if you need to specify the fields yourself: No you don't, you simply bind any of the values from the entity straight to a field. If the user fills them out, they will appear in the object used in the creation. You can retrieve the object at any time using

const filledOutEntityFields = myView.getBindingContext().getObject();

在您的视图中,您可以像往常一样使用相对绑定:

In your views you can use a relative binding like you always would:

<Input value="{myField}" />

或者更具体地说明您的类型和类型检查:

Or to be more specific with your types and type checking:

<Input value="{
  path: 'myField', 
  type: 'sap.ui.model.type.String',
  constraints: {
    minLength: 1,
    maxLength: 20
  }
}" />

稍后您触发创建.如果你使用了一个 changeSet,你应该在那里传递它的名字.

And later on you trigger the create. If you used a changeSet, you should pass its name in there.

this.getModel().submitChanges('myChanges');

优点之一是,除了不必使用 JSON 模型之外,如果您检索此数据并将元素与现有结果绑定,则代码是相同的.你也可以使用 submitChanges .除了,它发送更新而不是创建.

One of the advantages is, besides not having to use a JSON model, if you retrieve this data and bind the element with existing results, the code is the same. You can use submitChanges on that, too. Except, it sends an update and not a create.

更多信息:https://ui5.sap.com/sdk#/api/sap.ui.model.odata.v2.ODataModel/methods/createEntry

这篇关于如何在新实例化期间为实体创建 odata 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆