SAPUI5 Oda Vch Ed

SAPUI5 OData V4 Read

请问如何在 SAPUI5 的 OData V4 中执行 OData 读取?

我可以在 OData V2 中很容易地做到这一点,我如何使用 oData V4 实现类似的事情?

this_.getOwnerComponent().getModel("myModel").read("/zprojects", {
                "async": true,
                "success": function (oData) {
                    console.log(oData);
                },
                "error": function (oError) {
                    console.log(oError);
                }
            });

我的问题是我想在绑定到视图之前处理数据以添加额外的值。这是我的 oData V2 完整代码:

this_.getOwnerComponent().getModel("myModel").read("/zprojects", {
                "async": true,
                "success": function (oData) {
                    var myArray = [];
                    var pos;

                    for (var i = 0; i < oData.results.length; i++) {
                        pos = myArray.map(function (e) {
                            return e.ID;
                        }).indexOf(oData.results[i].PROJECTID);
                        if (pos === -1) {
                            myArray.push({
                                ID: oData.results[i].PROJECTID,
                                PROJECT_DESC: oData.results[i].PROJECT_DESC
                            });
                        }
                    }
                    myArray.sort((a, b) => (a.PROJECT_DESC > b.PROJECT_DESC) ? 1 : -1);
                    myArray.unshift({
                        ID: "-1",
                        PROJECT_DESC: "Please select Project ID"
                    oModel = new sap.ui.model.json.JSONModel(myArray);
                    sap.ui.core.Fragment.byId("idFragment", "project").setModel(oModel);
                },
                "error": function (oError) {
                    console.log(oError);
                }
            });

来自documentation

The OData V4 model only supports data access using bindings. It does not provide any direct access to the data.

您可以通过创建假绑定并监听 dataReceived 事件来解决这个问题,但我宁愿建议使用 jQuery's ajax features 来请求数据,直到 v4.ODataModel 支持直接访问数据:

$.get({
    url: "<your_service_url>/zprojects",
    success: function(data) {
        // your success logic
    },
    error: function(error) {
        // your error logic
    }
});