简体   繁体   中英

SAPUI5 get OData model in controller

i'm making simple project in SAPUI5 and i have some problems with gettin data from OData in controller. I'm guessin if it is possible in js.

I am connected with Northwind.svc by destination. I can easily show it in .view file using just "{/Products}", but nothing works in .controller file. I tried to use this.getView().getModel(), creating new sap.ui.model.odata.v2.ODataModel({serviceUrl: "services.odata.org/Northwind/Northwind.svc" });

All the time i want to get propierties i'm just getting null back.

There is piece of my manifest.json file

"sap.app": {
    "dataSources": {
        "Northwind": {
            "uri": "/V2/Northwind/Northwind.svc/",
            "type": "OData",
            "settings": {
                "odataVersion": "2.0",
                "localUri": "localService/Northwind/metadata.xml"
            }
        }
    }
},

"models": {
        "i18n": {
            "type": "sap.ui.model.resource.ResourceModel",
            "settings": {
                "bundleName": "Odczyty.i18n.i18n"
            }
        },
        "": {
            "type": "sap.ui.model.odata.v2.ODataModel",
            "settings": {
                "defaultOperationMode": "Server",
                "defaultBindingMode": "Default",
                "defaultCountMode": "Request"
            },
            "dataSource": "Northwind",
            "preload": true
        }

neo-app.json:

"routes": [
{
  "path": "/resources",
  "target": {
    "type": "service",
    "name": "sapui5",
    "entryPath": "/resources"
  },
  "description": "SAPUI5 Resources"
},
{
  "path": "/test-resources",
  "target": {
    "type": "service",
    "name": "sapui5",
    "entryPath": "/test-resources"
  },
  "description": "SAPUI5 Test Resources"
},
{
  "path": "/V2/Northwind/Northwind.svc/",
  "target": {
    "type": "destination",
    "name": "Northwind"
  },
  "description": "Northwind OData Service"
}

]

Component:js

init: function() {
        // call the base component's init function
        UIComponent.prototype.init.apply(this, arguments);

        // set the device model
        this.setModel(models.createDeviceModel(), "device");
    }

models.js:

return {

    createDeviceModel: function() {
        var oModel = new JSONModel(Device);
        oModel.setDefaultBindingMode("OneWay");
        return oModel;
    }
};

Any ideas what i'm doing wrong?

You can use getObject to get your object or property from ODataModel , for example, you can get Email property by following:

oModel.getObject("/Clients(KEY)/Email")

Or you can get the whole object first then access the Email property

oModel.getObject("/Clients(KEY)").Email

KEY should be 9000005L in your sample response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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