Fail when load OData in SAPUI5

别来无恙 提交于 2019-12-11 05:10:19

问题


I have configured the information for accessing my OData Service from SAP UI5 in the manifest.json

{
"sap.app": { ...
    },
    "dataSources": {
        "Test": {
            "uri": "/sap/opu/odata/sap/ZHCM_SRV/",
            "type": "OData",
            "settings": {
                "odataVersion": "2.0",
                "localUri": "localService/metadata.xml"
            }
        }
    } ...
"sap.ui5": {
    "rootView": {
        "viewName": "test.view.App",
        "type": "XML",
        "id": "app"
    }, ...
    "models": {
        "i18n": {
            "type": "sap.ui.model.resource.ResourceModel",
            "settings": {
                "bundleName": "test.i18n.i18n"
            }
        },
        "Test": {
            "type": "sap.ui.model.odata.v2.ODataModel",
            "settings": {
                "defaultOperationMode": "Server",
                "defaultBindingMode": "TwoWay",
                "defaultCountMode": "None"
            },
            "dataSource": "Test"
        }
    },
    "routing": { ...
}

In my Component.js I have:

init: function() {

        UIComponent.prototype.init.apply(this, arguments);

        this.getRouter().initialize();

        this.setModel(models.createDeviceModel(), "device");

        // I get the Url from configuration in Manifest.json
        var sServiceUrl = this.getMetadata().getManifestEntry("sap.app").dataSources["Test"].uri;

        // I create the OData 
        var sabModel = new sap.ui.model.odata.v2.ODataModel(sServiceUrl);

        // Load the MetaData.
        sabModel.getServiceMetadata();

        // Binding to the Model.
        this.setModel(sabModel, "Sabbatical");

when I am checking the oModel I can see I have instantiated the Model but it is empty, no data... Have you had this behavior? Any suggestion???

Thanks a lot!! CBR


回答1:


OData models don't contain any data from entities initially. It's only the metadata that is requested during the instantiation of the model.

Entity sets can be gathered in following ways:

  • Manually via CRUD APIs
  • Let UI5 handle requests automatically according to the aggregation or element binding definition (preferred):

    Requests to the back end are triggered by list bindings, element bindings, and CRUD functions provided by the ODataModel. Property bindings do not trigger requests.

Take a look at the documentation OData V2 Model.
Here is an example from a similar answer: https://embed.plnkr.co/wAlrHB/


Note: Neither getProperty nor getObject sends any request but returns a copy of the value from the cache.




回答2:


What you have done so far is: set up a connection to the model.

If you want to fill it with data you can do so by either binding the model to a UI element, e.g. a table, or by reading the data programmatically: sap.ui.model.Model getProperty().

In your case you'd call

var yourVariable = sabModel.getProperty('path/to/your/property');


来源:https://stackoverflow.com/questions/46659612/fail-when-load-odata-in-sapui5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!