简体   繁体   English

SAPUI5:如何以编程方式访问ODataModel数据?

[英]SAPUI5: How can I access ODataModel data programatically?

When I create a model in code, I usually use: 在代码中创建模型时,通常使用:

var oData = {
  "name" : "",
  "description" : "",
  "phone" : ""
};
var oModel = new JSONModel(oData);
this.setModel(oModel, "data");

After that, I can access to the model and it's values using: 之后,我可以使用以下方法访问模型及其值:

var oModel = this.getView().getModel("data");
var description = oModel.getProperty("/description");

But, that is using an internal json structure. 但是,那是使用内部json结构。 How can I get the oModel structure when I use a oData destination from Hana Platform or when I use a mockup-server on my SAPUI5 project? 当我使用Hana Platform的oData目标或在SAPUI5项目中使用模型服务器时,如何获得oModel结构?

The getProperty method exists on an ODataModel (v2) as on any other model. 与其他模型一样, getProperty方法也存在于ODataModel(v2)上。 Hence the usage differs a lot: 因此用法有很大不同:

How ODataModel stores data ODataModel如何存储数据

This is because the ODataModel stores data by it's keys, eg 这是因为ODataModel通过其键存储数据,例如

{
  "EntitySet('Key-1')": {},
  "EntitySet('Key-2')": {},
  "EntitySet('Key-3')": {},
  "ExpandedEntitySet(EntityID='Key-3',ExpandedEntityIS='5')": {}
}

Check oMyODataDataModel.oData to see the actual stored data (but please do not use or manipulate it this way since this is internal API). 检查oMyODataDataModel.oData以查看实际存储的数据(但是,由于这是内部API,请不要以这种方式使用或操作它)。

ODataModels getProperty ODataModels getProperty

To retrieve a single entity you would have to say something like: 要检索单个实体,您必须说出类似以下内容:

oDataModel.getProperty("/EntitySet('Key-1')");

Eventhough the binding path for the collection is /EntitySet requesting 尽管集合的绑定路径是/EntitySet请求

oDataModel.getProperty("/EntitySet");

would return nothing. 什么也不会返回。 This happens because there is no entity in the internally stored data structure for oData["EntitySet"] and the getProperty method is still nothing else then a look-up in this internal structure. 之所以会发生这种情况,是因为内部存储的数据结构中没有用于oData [“ EntitySet”]的实体,而getProperty方法仍然只是对该内部结构的查找。

Include expanded entities 包括扩展实体

One interesting thing with ODataModels getProperty method is the bIncludeExpandEntries parameter. 使用ODataModels的getProperty方法的一件有趣的事情是bIncludeExpandEntries参数。 If you set it to true the accessed entity will be returned including all potentially expanded NavigationProperties. 如果将其设置为true,则将返回访问的实体,包括所有可能扩展的NavigationProperty。 In the above example 在上面的例子中

oDataModel.getProperty("/EntitySet('Key-3')");

will also return "ExpandedEntitySet(EntityID='Key-3',ExpandedEntityIS='5')" with it. 还将返回“ ExpandedEntitySet(EntityID ='Key-3',ExpandedEntityIS ='5')”。

ODataModels getObject ODataModels getObject

ODataModels getObject method has a lot more flexibility since it allows for the local use of the OData parameters $select and $expand . ODataModels getObject方法具有更大的灵活性,因为它允许在本地使用OData参数$select$expand Getting an EntitySet is still not possible... NOTE: It will not load any missing data and the returned data may be incomplete! 仍然无法获得EntitySet ...注意:它不会加载任何丢失的数据,并且返回的数据可能不完整!

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

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