cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to read JSON properly

Former Member
0 Kudos

Hi Community,


Here is my config.js


I am buillding a little rolecontrol here. My problem is, that i can't ready my JSON Objects right. i dont.


The weird point is, that when ever I try my code in the Browserconsole it works. When i use it in my actual Code, it always gives me undiefined, null or an empty Object (Object {} ).


Here is my Code:


Index.html


<!DOCTYPE HTML>

<html>

  <head>

  <meta http-equiv="X-UA-Compatible" content="IE=edge">

  <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>

  <script  id="sap-ui-bootstrap" type="text/javascript"

  src="resources/sap-ui-core.js"

      data-sap-ui-theme="sap_bluecrystal"

      data-sap-ui-xx-bindingSyntax="complex"

      data-sap-ui-libs="sap.m,sap.ui.layout, sap.ui.commons, sap.ui.table">

  </script>

  <script src="config/config.js" type="text/javascript" ></script>

  <script>

  sap.ui.localResources("view");

  var app = new sap.m.App("RootView",{initialPage:"MainPage"});;

  var page1 = sap.ui.view({id:"MainPage", viewName:"view.entry", type:sap.ui.core.mvc.ViewType.JS});

  var page2 = sap.ui.view({id:"Logbook", viewName:"view.Logbook.Logbook", type:sap.ui.core.mvc.ViewType.JS});

  var profileModel = new sap.ui.model.json.JSONModel("model/getCurrentProfile.json");

  sap.ui.getCore().setModel(profileModel, "profile");

  setRole();

  app.addPage(page1).addPage(page2);

  app.placeAt("content");

  </script>

  </head>

  <body class="sapUiBody" role="application">

  <div id="content"></div>

  </body>

</html>


Here is my config.js

//Configuration

jQuery.sap.declare("main.app.config");

jQuery.sap.require("sap.ui.model.json.JSONModel");

main.app.config = {

  visible : false

};

function setRole() {

  pModel = sap.ui.getCore().getModel("profile")

  console.log("pModel BELOW");

  console.log(pModel);

  console.log("________________________");

  console.log("pModel.getData() BELOW");

  console.log(pModel.getData());

  console.log("________________________");

  // switch (pModel.oData[0].role) {

  //    case "admin":

  //        break;

  //    case "user":// 

  //        break;

  //    case "user2":

  //        break;

  //    default:

  // }

}

And my JSON file:


[

    {

        "firstname": "Mister",

        "lastname": "Test",

        "email": "test@mail.de",

        "language": "en",

        "role": "admin"

    }

]

In the Screenshots I will show this, for me, unexplainable behavouir:

I hope some of you have an Idea why this is not working.

Regards,
Dain

Accepted Solutions (1)

Accepted Solutions (1)

former_member182372
Active Contributor
0 Kudos

here we go again, told you, every week!

former_member182862
Active Contributor
0 Kudos

Can you write a blog and we always refer to it for questions like this? :slightly_smiling_face:

Former Member
0 Kudos

So far that help really good, thank you!

But now I'm facing a new issuse.
I wanted to set this whole thing up, to get global models, to use them in every controller or view i got in my solution.

when I now trie to use

sap.ui.getCore().getModel("profile")

in a diffrent view, its always undefined.

I found out that the Problem is, that CreateContet() is called before attachRequestCompleted().

Do you have any examples or tips of how I can set global Model?

former_member182372
Active Contributor
0 Kudos

fire event in attachRequestCompleted and subscribe to it in your views, so view will be notified when data is loaded

sap.ui.getCore().getEventBus().publish("app", "JSONDATAisLoaded");

Former Member
0 Kudos

The Problem is that i cant even create the views to notify them.

To Create them i need the data out of my model

former_member182372
Active Contributor
0 Kudos

in createContent create just page or app and the dynamic logic place in event handler

one more option is to pass third parameter of loadData as false (bAsync) so it load data synchronosly, but I REALLY-REALLLY recommend to avoid that as it will freeze the browser window till data is loaded

former_member182862
Active Contributor
0 Kudos

HI Dain

We have to separate the view and the model. Do not strongly couple them together.

-D

Answers (0)