cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 model in manifest not available to view on first pass lifecycle hooks

former_member182500
Contributor
0 Kudos

Hi,

Have defined a model in the manifest.json:

"models": {

     "tiles": {

            "type": "sap.ui.model.json.JSONModel",

            "uri": "model/tiles.json"

       }

  }

tiles.json looks like this, purpose is to define a set of tiles which will influence UI control content of a view

{

  "tiles": {

       "tile": [{

            "id": "GT1",

            "type": "genericTile1"

       }]

  }

}

Problem

On first pass of the lifecycle hooks in app.controller.js - onInit, onBeforeRendering, onAfterRendering - the model is not available to the view with

var mTileInfo    = this.getView().getModel("tiles");

Results in mTileInfo undefined. As stated earlier, the model is to be used to influence the control content of the view before rendering.

Would seem that during first pass of hooks the view is not yet placed in the control root, and therefore at this point the component is not the parent where the model has been instantiated?

Tyhe model is available within the view after view has been rendered, checked in debug upon raising of event for button for example, but by this time too late to influence first view render.

Grateful for suggestions on how to resolve, many thanks

Former Member

View Entire Topic
jamie_cawley
Advisor
Advisor
0 Kudos

In your onInit try using

this.getOwnerComponent().getModel("tiles");

Regards,

Jamie

SAP - Technology RIG

former_member182500
Contributor
0 Kudos

Hi Jamie,

Many thanks for the reply and help, unfortunately it does not give me what I'm trying to achieve.

Model after using your suggestion in onInit of app controller in lifecycle first pass - in oData of model  there is not tiles object or tile array.

Placing a button on the view simply to view the odata of model after first complete lifecycle pass, we can clearly see the tiles object (defined in tiles.json).  My goal is to achieve this fully qualified odata property during first pass to control view content as determined by the json configuration:

Any ideas?  Thanks very much.

jamie_cawley
Advisor
Advisor
0 Kudos

Are you using


onInit: function(){

  var mTileInfo = this.getOwnerComponent().getModel("tiles");

  console.log(mTileInfo);

  }

I don't think use the debugger is an accurate way of verifying this, it will not let the call complete.

Regards,

Jamie

SAP - Technology RIG

saivellanki
Active Contributor
0 Kudos

Hi Jon,


Can you try the same what Jamie has suggested at onAfterRendering event?


onAfterRendering: function(){

     var oComponent = this.getOwnerComponent();

     var oModel = oComponent.getModel("tiles");

     var oModelData = oModel.getData();

}

Regards,

Sai Vellanki.

former_member182500
Contributor
0 Kudos

Thanks Jamie, much appreciated, all good

former_member182500
Contributor
0 Kudos

Hi

Just following up on proposal to use:

var mTileInfo = this.getOwnerComponent().getModel("tiles");

Writing contents of mTileInfo shows tiles model in odata property:

console.log(mTileInfo);

In order to access model properties, I use the below within onInit of the controller. Unfortunately returns undefined

console.log(mTileInfo.getProperty("/tiles/tile"));

However If I declare a variable with exact same JSON notation, for example, and write out property, all is good.  Grateful for suggestion.

var data = {
"tiles": {
"tile": [{
"id": "GT1",
"type": "genericTile1"
}]
}
}

var gConfigModel = new sap.ui.model.json.JSONModel(data);

console.log(gConfigModel.getProperty("/tiles/tile"));
jamie_cawley
Advisor
Advisor
0 Kudos

It's an asynchronous call, so you could do

mTileInfo.attachRequestCompleted(function() {

       console.log(mTileInfo.getProperty("/tiles/tile"));

});

Regards,

Jamie

SAP - Technology RIG

former_member182500
Contributor
0 Kudos

Awesome Jamie, thanks very much (again!)