5

I wrote this part to bind OData information with a select controller:

var countrItems = new sap.ui.core.ListItem();
countrItems.bindProperty("key", "Land1");
countrItems.bindProperty("text", "Landx");
var CountrSelect = this.byId("CountrySelect");
CountrSelect.setModel(oModelTriptab);
CountrSelect.bindItems("/Countries", countrItems);

I would like to perform an action after the binding is complete (I want to select some default value that can change dynamically).

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
ducks13
  • 81
  • 2
  • 3
  • 10

2 Answers2

19

Attach handlers to events provided by Binding:

<Select items="{
  path: '/Countries',
  events: {
    dataRequested: '.onCountriesRequested',
    dataReceived: '.onCountriesReceived',
    change: '.onCountriesChange'
  }
}">

Those events can be applied, not only to ListBindings, but to all bindings. Note: PropertyBindings do not trigger any requests, so no dataRequested or dataReceived for them.

I would like to perform an action after the binding is complete.

In that case, you'd attach a handler either to change or dataReceived event to be notified about the "complete" binding.

Compared to attaching a handler to requestCompleted, the above approach is more descriptive and, most importantly, binding-specific.


List, m.Table, m.Tree, ...

Alternatively, UI5 offers the event updateFinished for Controls derived from sap.m.ListBase. It's similar to the change event, but provides more information such as an additional "Growing" reason, actual, and total count of the entries.

<List
  updateFinished=".onCountriesUpdateFinished"
  items="..."
>
onCountriesUpdateFinished: function(event) {
  const reasonForUpdate = event.getParameter("reason"); // "Refresh", "Growing", "Filter", "Sort", "Context", ...
  const currentlyLoadedNumberOfCountries = event.getParameter("actual");
  const totalNumberOfCountries = event.getParamter("total") // Value of $count
  // ...
}
Community
  • 1
  • 1
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
3

Use the model's requestCompleted event handler to perform any actions that should happen right after your model data is updated.

The binding itself should be rather static (i.e. it will not change) so you'r only interested in when the data is changed


edit here's an example implemantation:

var that = this;
oModelTriptab.attachRequestCompleted(function(oEvent){
    var oSelect = that.byId("CountrySelect");
    oSelect.setSelectedKey("whatever");
});

See https://openui5.hana.ondemand.com/docs/api/symbols/sap.ui.model.Model.html#attachRequestCompleted for more info

Qualiture
  • 4,900
  • 7
  • 27
  • 38
  • This is not a safe solution. I used this and it may happen the request completed while the items are not rendered yet! – MJBZA Sep 04 '20 at 10:46
  • It is a perfectly safe solution. I think you confuse the actual request being completed on the *model* (which is what OP is after) vs the *rendering* being completed (which is something entirely different and not related to the model). EDIT: having said that, this answer is more than 5 years old and the one below by Boghyon iswhat I prefer too – Qualiture Sep 04 '20 at 10:50
  • 1
    I mean using `dataReceived` is a better event to handle this situation. It may happen that the `RequestCompleted` happen and after that `DataReceived` happen. – MJBZA Sep 04 '20 at 11:03
  • Yes, absolutely agree, `dataReceived` on the binding should be the prefered solution – Qualiture Sep 04 '20 at 11:07