Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

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

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

Former Member
Former Member replied


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*

1 View this answer in context
Not what you were looking for? View more on this topic or Ask a question