标题:SAPUI5 使用 OData - 在 LaunchPad 视图中显示植物数量

问题描述 投票:0回答:1

我正在使用 CAP Business Application Studio (BAS) 开发 SAPUI5 应用程序,并且我有一个 LaunchPad 视图,其中包含导航到 PlantsList 视图的磁贴。我想使用 OData 服务在 LaunchPad 视图中显示植物数量。

如何正确使用LaunchPad控制器中的OData服务来对“Plants”实体执行$count操作?

在使用 OData 服务和执行计数操作时,我需要注意 SAPUI5 或 CAP BAS 中的任何具体注意事项或配置吗?

LaunchPad.控制器

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/core/routing/History",
    "sap/ui/model/json/JSONModel"
], function (Controller, History, JSONModel) {
    "use strict";

    return Controller.extend("farm.Farm.controller.LaunchPad", {
         
        onInit: function() {
            this._bindPlantCount();
        },

        _bindPlantCount: function() {
            const oModel = this.getOwnerComponent().getModel();
            
            oModel.read("/Plants/$count", {
                success: function (oData) {
                    const iPlantCount = parseInt(oData, 10);
                    const oViewModel = new JSONModel({
                        plantCount: iPlantCount
                    });
                    this.getView().setModel(oViewModel, "view");
                }.bind(this),
                error: function () {
                    // Handle error
                }
            });
        },
         
        onPress: function () {
            this.getOwnerComponent().getRouter().navTo("PlantList");
        },

        onNavBack: function () {
            var sPreviousHash = History.getInstance().getPreviousHash();
            if (sPreviousHash !== undefined) {
                history.go(-1);
            } else {
                this.getOwnerComponent().getRouter().navTo("Login", {}, true);
            }
        }
    });
});

LaunchPad.view

<mvc:View controllerName="farm.Farm.controller.LaunchPad" xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" xmlns:core="sap.ui.core">
    <core:Fragment fragmentName="farm.Farm.view.Header" type="XML"/>
    
    <GenericTile class="sapUiTinyMarginBegin sapUiTinyMarginTop tileLayout" header="Farmbot" subheader="Plants List" press="onPress">
        <TileContent unit="" footer="">
            <NumericContent value="{plantsModel>/plantsCount}" withMargin="false"/>
        </TileContent>
    </GenericTile>
</mvc:View>

清单.json

{
    "_version": "1.12.0",
    "sap.app": {
        "id": "farm.Farm",
        "type": "application",
        "i18n": "i18n/i18n.properties",
        "applicationVersion": {
            "version": "1.0.0"
        },
        "title": "{{appTitle}}",
        "description": "{{appDescription}}",
        "dataSources": {
            "mainService" : {
                "uri": "odata/v4/catalog/",
                "type": "OData",
                "settings": {
                    "annotations": [],
                    "localUri": "localService/metadata.xml",
                    "odataVersion":"4.0"
                }
            }
        },
        "sourceTemplate": {
            "id": "ui5template.basicSAPUI5ApplicationProject",
            "version": "1.40.12"
        }
    },

    "sap.ui": {
        "technology": "UI5",
        "icons": {
            "icon": "",
            "favIcon": "",
            "phone": "",
            "phone@2": "",
            "tablet": "",
            "tablet@2": ""
        },
        "deviceTypes": {
            "desktop": true,
            "tablet": true,
            "phone": true
        }
    },

    "sap.ui5": {
        "flexEnabled": false,
        "rootView": {
            "viewName": "farm.Farm.view.App",
            "type": "XML",
            "async": true,
            "id": "App"
        },
        "dependencies": {
            "minUI5Version": "1.65.6",
            "libs": {
                "sap.ui.layout": {},
                "sap.ui.core": {},
                "sap.m": {}
            }
        },
        "contentDensities": {
            "compact": true,
            "cozy": true
        },
        "models": {
            "i18n": {
                "type": "sap.ui.model.resource.ResourceModel",
                "settings": {
                    "bundleName": "farm.Farm.i18n.i18n"
                }
            },
            "": {
                "type": "sap.ui.model.odata.v4.ODataModel",
                "dataSource": "mainService",
                "preload": true,
                "settings": {
                    "synchronizationMode": "None",
                    "operationMode": "Server",
                    "autoExpandSelect": true,
                    "earlyRequests": true,
                    "groupId": "$auto"
                }
            }
            
        },
        "resources": {
            "css": [{
                "uri": "css/style.css"
            }]
        },
        "routing": {
            "config": {
                "routerClass": "sap.m.routing.Router",
                "viewType": "XML",
                "async": true,
                "viewPath": "farm.Farm.view",
                "controlAggregation": "pages",
                "controlId": "app",
                "clearControlAggregation": false
            },
            "routes": [{
                "pattern": "/",
                "name": "Login",
                "target": ["login"]
            }, {
                "pattern": "launchpad",
                "name": "launchpad",
                "target": ["launchpad"]
            },
            {
                "pattern": "PlantList",
                "name": "PlantList",
                "target": ["plantList"]
            },
            {
                "pattern": "Details/{Plants/ID}",
                "name": "Details",
                "target": ["details"]
            }],
            "targets": {
                "login": {
                    "viewName": "Login",
                    "viewLevel": 1
                },
                "launchpad": {
                    "viewName": "LaunchPad",
                    "viewLevel": 2
                },
                "plantList": {
                    "viewName": "PlantList",
                    "viewLevel": 3
                },
                "details": {
                    "viewName": "Details",
                    "viewLevel": 4
                }

            }
        }
    }
}

我尝试使用OData模型的read方法对“Plants”实体执行$count操作。我希望检索植物的数量并将其绑定到 JSONModel 以在 LaunchPad 视图中显示。但是,计数没有按预期检索,并且我未能获取模型

odata sapui5 cds sap-business-application-studio
1个回答
0
投票

在您的 launchpad.controller 中,您使用“view”别名将 oViewModel 设置为视图作为名称模型。

            this.getView().setModel(oViewModel, "view");

稍后在 LaunchPad.view 中,您将使用 plantModel 作为 json 模型而不是“视图”。

始终使用相同的模型别名来确保一致性。如果您打算使用“view”作为模型别名,请确保更新 LaunchPad.view 以反映此更改:

<mvc:View controllerName="farm.Farm.controller.LaunchPad" xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc" xmlns:core="sap.ui.core">
    <core:Fragment fragmentName="farm.Farm.view.Header" type="XML"/>
    
    <GenericTile class="sapUiTinyMarginBegin sapUiTinyMarginTop tileLayout" header="Farmbot" subheader="Plants List" press="onPress">
        <TileContent unit="" footer="">
            <NumericContent value="{view>/plantsCount}" withMargin="false"/>
        </TileContent>
    </GenericTile>
</mvc:View>
© www.soinside.com 2019 - 2024. All rights reserved.