问题
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