MLauffer
MLauffer

Reputation: 23

SAPUI5 oModel.getJSON() isn't working when I use a JSON file as origin

I created my Model using a JSON file.

var oModel = new sap.ui.model.json.JSONModel( jsonFileUrl ); //JSON from file

It worked and the element was populated as I wanted. But after this, when I tried to use oModel.getJSON() to get the JSON data it didn't work.

If I use an variable with the same content as the file, it works!

You can check the full test that I created: https://googledrive.com/host/0B2gJUv6a_V1dYnNSV0ZsTFhxazg/index.html

Is there anybody to help me to understand what on Earth is happening here?

Upvotes: 1

Views: 1589

Answers (1)

qmacro
qmacro

Reputation: 3105

It's because at the time you try to emit the JSON here:

$("#jsonFile").append(oModelFile.getJSON());

the actual ajax request to retrieve the file hasn't completed, and so the JSON model isn't filled at that time.

Wrap this in a handler for the requestCompleted event like this and it will work:

oModelFile.attachRequestCompleted(function() {
  $("#jsonFile").append(oModelFile.getJSON());
});

Upvotes: 1

Related Questions