Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ODataModel passing "expand" parameter in read

Tags:

odata

sapui5

I'd like to pass expand parameters to read because it doesn't work if I call the service like this:

oModel1.read("/LinesSet?$expand=ToCells", {
like image 692
Mahdi Hammami Avatar asked Dec 08 '22 18:12

Mahdi Hammami


1 Answers

The read API awaits a map of options as a second argument in which we can define any query using the property urlParameters:

oModel1.read("/LinesSet", {
  urlParameters: {
    "$expand": "ToCells",
    "$select": "LineID,ToCells/CellID,...", // reduce data load
  },
  filters: [ // Filter required from sap/ui/model/Filter
    new Filter({/*...*/}), // reduce data load
  ],
  success: this.onSuccess.bind(this),
  // ...
});

⚠️ Please note that loading large amounts of data significantly affects memory consumption and UX negatively. This might even lead to crashing the application altogether ultimately. See the section Loading Large Amounts of Data from the documentation.

Whenever you use methods like [...] sap.ui.model.odata.v2.ODataModel#read [...] in application code, your application must not load large amounts of data.

⚠️ read is a low-level API from the application's point of view. There are other APIs and approaches that can help reducing the amount controller code.


Alternative (better) solution

I'd like to emphasize that v2.ODataModel#read is often not required. You can simply make use of the OData Context/ListBinding by assigning the corresponding name of the <NavigationProperty> to the control in XML:

<Table binding="{ToThatRelatedSingleEntity}" items="{ToThatRelatedCollection}" growing="true">

(Note: You might have to add templateShareable to the aggregation binding accordingly as explained in the topic: Lifecycle of Binding Templates)

The binding, not the application, will then prepare a request automatically for you. No need to use an intermediate JSONModel. Same with v4.ODataModel which doesn't even have the read method.
This makes also migrating to OData V4 much easier.

like image 60
Boghyon Hoffmann Avatar answered Apr 29 '23 20:04

Boghyon Hoffmann