最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SAPUI5 get OData model in controller - Stack Overflow

programmeradmin2浏览0评论

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/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 ponent'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?

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/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 ponent'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?

Share Improve this question asked Aug 10, 2017 at 9:55 codesyntaxcodesyntax 331 gold badge3 silver badges7 bronze badges 4
  • 2 Hello, try call this.getOwnerComponent().getModel() in your controller. – Haojie Commented Aug 11, 2017 at 1:32
  • Hi, thank you for response! Already found it yesterday and it really works, but here is another problem - when i'm trying to get some properties it returning me just empty object. I've tried with oModel.getProperty("/") oModel.getProperty("/Clients(1)/Id"); oModel.getData("/"); And some other tricks but nothing returns data. – codesyntax Commented Aug 11, 2017 at 6:25
  • Could you please specify what object and property you want to get from NorthWind by Model? – Haojie Commented Aug 11, 2017 at 6:29
  • Ahh, didn't tell u.. Yesterday i've decided to connect my friends odata in eclipse. Now the structure looks like: pastebin./F1vDaqRH – codesyntax Commented Aug 11, 2017 at 6:56
Add a ment  | 

1 Answer 1

Reset to default 2

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

发布评论

评论列表(0)

  1. 暂无评论