0
votes

I am implementing a Master-Detail App using SAPUI5, when open the app at first time, URL hash is empty, and I want set the hash to the first item in the master list, but I cannot fire the dataReceived event to decide the listLoading is done. Any idea?

manifest.json:

{
    "sap.app": {
        "dataSources": {
            "mainService": {
                "uri": "XXX",
                "type": "JSON"
            }
        },
        ...
}

Master.view.xml:

<List items="{path: '/'}">
        ...
</List>

Master.controller.js

onInit : function () {
    this.getRouter().getRoute("master").attachPatternMatched(this._onMasterMatched, this);
}
/**
* If the master route was hit (empty hash) we have to set
* the hash to to the first item in the list as soon as the
* listLoading is done and the first item in the list is known
* @private
*/
_onMasterMatched :  function() {
    console.log(this._oList.getBinding("items") instanceof sap.ui.model.Binding) // return ture
    this._oList.getBinding("items").attachEventOnce("dataReceived",
        function(oData) {
            //did not call 
            var oFirstListItem = this._oList.getItems()[0]; 
        });
}

Ref: https://sapui5.hana.ondemand.com/#docs/api/symbols/sap.ui.model.Binding.html#event:dataReceived

2
In contrast to dataReceived, the event updateFinished works even with a JSONModel no matter when the data are actually received: stackoverflow.com/a/43970340/5846045Boghyon Hoffmann

2 Answers

0
votes

From the code above I can assume that you have a JSONModel which is created by the Component via app descriptor (=manifest.json), so we are talking about "automatic" creation of the model. The JSONModel loads the data successfully I guess... However, all this happens at a very early stage of the app. In other words, your event handler will never be called because the data is already loaded and attaching event handlers after the data has been loaded meanse you might be attaching the event handler too late.

By the way: Instead of attaching the handler (everytime) inside _onMasterMatched() I would suggest to do it in onInit() directly by using attachEvent(). This is just a hint, it would not help in your case.

Suggestion: How about you create in JSONModel inside your Master.controller.js in onInit()? Here you can then easily attach the event handler. I would even prefer this because the data for the master list belongs to the Master View/Controller and therefore I would not put it into the app descriptor. But since I am not really aware of the requirements I might be wrong here...

0
votes

Another way to get notified about the initial update of the list would be just to use updateFinished in the onInit handler.

onInit: function() {
  this.byId("list").attachEventOnce('updateFinished', this.onInitialUpdateFinished, this);
},

The event handler onInitialUpdateFinished will be fired when the list "received" and bound the data.