SAP Fiori Elements applies the model name decision logic in the OData metadata request url

question

I yarn startstarted a SAP Fiori Elements application locally, and observed an OData metadata request url in the network panel of the Chrome developer tool as follows:

http://localhost:8080/sap/opu/odata/sap/SEPMRA_PROD_MAN/$metadata?sap-value-list=none&sap-language=EN

How is the name of this OData service SEPMRA_PROD_MANdetermined at runtime?

Check out Component.jsthe functions in the file Component._createManifestModels:

We look at the variables mModelsand find that there are three fields:

where the value of the "" field comes from the field of the same name in the section manifest.jsonof the file :models

dataSourcePoints to the value of mainService, and the latter urifield is the url that initiates the OData metadata request when the Fiori Elements application is running:

In SAP UI5 application, manifest.jsonfile is an important configuration file. It acts as the application's descriptor, defining the application's metadata, models, services, i18n (internationalization), etc. We can think of it as a centralized configuration center, where all configurations are in one place, making the code easier to maintain and easier to understand.

In manifest.jsonthe file, dataSourcesthe section is a very important part, which defines how the application connects to the backend service. This area can declare one or more data sources, and each data source corresponds to a backend service. These services can be OData services or other types of services.

After we manifest.jsondefine the data source in the file, we can refer to the service by the name of the data source in other parts of the application without remembering the specific URL. The advantage of this is that if the service's URL changes, we only need to update it in one place, rather than having to search the entire codebase to find all the places where the service is used.

The following is an example of regions manifest.jsonin a file :dataSources

{
    
    
    "sap.app": {
    
    
        "dataSources": {
    
    
            "mainService": {
    
    
                "uri": "/sap/opu/odata/sap/ZMAIN_SRV/",
                "type": "OData",
                "settings": {
    
    
                    "odataVersion": "2.0"
                }
            },
            "secondService": {
    
    
                "uri": "/sap/opu/odata/sap/ZSECOND_SRV/",
                "type": "OData",
                "settings": {
    
    
                    "odataVersion": "2.0"
                }
            }
        }
    }
}

In the above example, we defined two data sources named mainServiceand secondService. Each data source has a uriproperty that defines the URL of the service. typeThe attribute defines the type of service, here we are using OData service. settingsThe object contains other configuration of the service, such as the version of OData.

After defining the data source, we can use this data source in other parts of the application. For example, we can modelsdefine a model in a region and associate this model with a data source:

{
    
    
    "sap.ui5": {
    
    
        "models": {
    
    
            "": {
    
    
                "dataSource": "mainService",
                "preload": true
            },
            "secondModel": {
    
    
                "dataSource": "secondService",
                "preload": true
            }
        }
    }
}

In the above example, we defined two models, which are respectively associated with the two data sources we defined earlier. In this way, when we use these two models in the application, SAP UI5 will automatically use the corresponding data source to connect to the back-end service.

Summarize

In general, the fields manifest.jsonin the file dataSourcesare used to define how the application connects to the backend services. It centralizes the configuration of services in one place, making the code more maintainable and easier to understand.

Guess you like

Origin blog.csdn.net/i042416/article/details/131829623