Search code examples
modelsapui5

SAPUI5 Expand the model in the manifest


I want to add a new model to the manifest.json. The model is supposed to be expanded.

https://myPath/services/myService.xsodata/SubPath?$expand=CustomerRef

So the Datasource is defined:

"dataSources": {
    "mainService": {
        "uri": "/myPath/services/myService.xsodata/",
        "type": "OData",
        "settings": {
            "odataVersion": "2.0",
            "localUri": "localService/myService.xsodata/metadata.xml"
        }
    }

And the model is defined:

"models": {
    "Customer": {
        "type": "sap.ui.model.odata.v2.ODataModel",
        "settings": {
            "useBatch": "false"
        },
        "dataSource": "mainService"
    }

How can I add the expand to the model?


Solution

  • The data cannot be expanded in the model definition itself. The expand parameter is to be used where the binding between the view and data is defined as mentioned in the documentation:

    Some of the parameters must not be included in every request, but should only be added to specific aggregation or element bindings, such as $expand or $select. For this, the binding methods provide the option to pass a map of parameters, which are then included in all requests for this specific binding.

    Here is an excerpt from the example https://embed.plnkr.co/wAlrHB/:

    <List items="{
      path: 'odataModel>/Products',
      parameters: {
        expand: 'Category, Supplier',
        select: 'ProductName, UnitsInStock, Category/CategoryName, Supplier/Country'
      },
      sorter: [
        {
          path: 'Category/CategoryName',
          group: true
        }
      ],
      filters: [
        {
          path: 'Supplier/Country',
          operator: 'EQ',
          value1: 'UK'
        }
      ]
    }">
      <ObjectListItem title="{odataModel>ProductName}" number="{odataModel>UnitsInStock}"/>
    </List>
    

    The binding (not the model) will then send a request ...

    Requests to the back end are triggered by list bindings (ODataListBinding)

    ... with the respective parameters appended by UI5:

    https://services.odata.org/V2/Northwind/Northwind.svc/Products?$format=json&$skip=0&$top=100&$orderby=Category/CategoryName%20asc&$filter=Supplier/Country%20eq%20%27UK%27&$expand=Category%2c%20Supplier&$select=ProductName%2c%20UnitsInStock%2c%20Category%2fCategoryName%2c%20Supplier%2fCountry

    JSON response