cancel
Showing results for 
Search instead for 
Did you mean: 

JSONModel data from a file can not be read in view controllers onInit method

dominik_auras
Participant
0 Kudos

Hi,

In an other discussion I marked out a problem that makes me crazy!

I will post it here as a new question for clear discussion about this problem.

Now I have a test application with following scenario:

In my Component.js in method "init" I setup 2 identical models.

The first from code and the second from a .json file:

This is my file:

In my view controllers "onInit" method I want to read the data of the model (from .json file):

But the result of getData() of the model created from .json file is empty!

But as you can see in the log the data is there in oModelTest02 .... I don't get it...

I searched a lot now but can't find any solution for this problem!

Why am I not able to get access to the data from my file though the data is obviously there?!

I need the data to build some elements of my view dynamically (for example via loop and to calculate some things).

As long as I don't know an alternative for this procedure (any ideas?) I have to get this working.

Thanks and kind regards,

Dominik

Accepted Solutions (1)

Accepted Solutions (1)

former_member183518
Active Participant
0 Kudos


The reason for your second model logging empty data when using oModelTest02.getData() is due to the data being load from a file as an async request. Meaning, the request would not have fetched the data at the point when the log statement is executed.


Well, but why does the oModelTest02 shows the data in its log ?

Lets take a simple example for better clarification. Say we have a function as below,

function logger(){

     var model = {oData : {}};    

     console.log(model);   // what would be the value ? 

     model.oData["Player"] = {name : "Roger", age :24 } ;

}

Now when the logger() is invoked, what would be the value 'model' during console.log ? Obviously, model.oData contains an empty Object.

But this is what will be shown via console.log.

Because In console.log, the Object values would be evaluated only upon expanding that object in the log !

Just hover on the blue icon in the right side for the tool *tip*

dominik_auras
Participant
0 Kudos

Thank you! That helped me thinking about the logic and finding the solution!

I checked the Model API again and noticed the attachRequestCompleted method.

I now can solve it via attaching an event handler and then use the method loadData.

var oModel = new JSONModel();

oModel.attachRequestCompleted(function(oEvent) { console.log(oEvent.getSource().getData()); });

oModel.loadData(jQuery.sap.getModulePath(sNamespace, oConfig.sprintsLocal));

So the model is there!

Answers (1)

Answers (1)

former_member182372
Active Contributor
0 Kudos

Dennis Seah

here we go again, told you, every week!

JSONModel not loading from URL