Skip to Content
avatar image
Former Member

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Oct 19, 2015 at 04:57 PM


    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*


    logger.JPG (14.4 kB)
    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      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! 😊

  • Oct 19, 2015 at 07:15 PM

    Dennis Seah

    here we go again, told you, every week!

    JSONModel not loading from URL

    Add comment
    10|10000 characters needed characters exceeded