0

I'm trying to set a data model inside my manifest.json within my webapp. Im using sapui5 and I'm quite new to it.

the resource I'm getting from my api is a jsonObject but somehow the model is not initiated properly. I checked the model with console.log() and it's empty. when I do the same with an jsonArray it is working.

I should mention that I use a mockserver.js

Here is the code I'm using.

manifest.json:

 "sap.app": {
            ...  
    "dataSources": {
                "invoiceRemote": {
                    "uri": "https://services.odata.org/V2/Northwind/Northwind.svc/",
                    "type": "OData",
                    "settings": {
                        "odataVersion": "2.0",
                        "localUri": "localService/metadata.xml"
                    }
                }
            }
        }  
    ...
"sap.ui5": {  
            ...  
             "models": {  
                    "i18n": {
                        "type": "sap.ui.model.resource.ResourceModel",
                        "settings": {
                            "bundleName": "MyInboxUI5.i18n.i18n"
                        }
                    },  
                    "invoice": {
                        "dataSource": "invoiceRemote"
                    }
    ...

and with JsonObject I mean a .json of this style:

{
    "field1": value1,
    "field2": value2,
    "field3": [  
    {  
    "field4": value4,  
    "field5": value5  
    },  
    {
   "field6": value6,  
   "field7": value7  
   } ]  
}

(that's the one not working)

and with JsonArray I meant

[  
   {  
   "field4": value4,  
   "field5": value5  
   },  
   {
   "field6": value6,  
   "field7": value7  
   }  
] 

(This one is working)

To check my model I used the simple console.log()
Component.js (part of it)

init: function() {
        console.log(this.getModel("invoice"));

        UIComponent.prototype.init.apply(this, arguments);
        this.getRouter().initialize();
      }

I did not post the mockserver.js or metadata.xml because I'm not sure it's that relevant and they take a lot of space.

So does anyone know if there is a way to load the model of a JsonObject inside the manifest.json? I'm aware that there are other possibilities to load the model that do work, but I'm only interestet in that specific case.

vega
  • 11
  • 3
  • where did you control your model if it's empty or not? more code please. – omer.ersoy Mar 16 '18 at 07:49
  • I did that in the component.js inside the init function. When I use the JsonArray I get a nice model as output in my console but with the JsonObject it's just an empty skeleton. – vega Mar 16 '18 at 09:23
  • but this way you can just log metadata. I mean I didn't use a mockserver before but it's oData based so you should run oData operations (read, create etc.) if you want to get some data, right? – omer.ersoy Mar 16 '18 at 10:40
  • It's still unclear what you're trying to achieve.. What is the "api" you're talking about? – Boghyon Hoffmann Mar 17 '18 at 09:08
  • Hm ok, sorry. I guess I'm just such a newbie that I thought it's clear. What I mean with api is just an url, in my example the one below "dataSources" in my first code snippet. I should mention though, that the url I'm going to use as soon as I stop using my mockserver is going to be different and yields a JsonObject. The url written above is just for testing the mockserver. – vega Mar 19 '18 at 08:45

1 Answers1

0

Without having additional information about what you actually try to achieve it's hard to point you into the right direction.

The important information is that you are using an ODataModel + a mockserver. Thanks to the mockserver you can easily mock your data for the entities of your OData service - actually you can even mock much more...

Basically, the mock data files need to contain flat lists. In other words, you have always an array of flat objects. The mockserver gets the data (i.e. entities by id) from exactly from these files. The mockserver can only find the files if they have the correct name (see walkthrough tutorial for details). As a rule of thumb "1 file contains data for one entity/entityset".

There is no way to model JsonObjects inside the manifest. What you can do is mocking your mockserver (i.e. by reading json files manually), that works perfectly (the explored app has some examples). However, don't forget we are talking about OData!

Hint: your data looks like a tree, so I guess you want to model a tree structure. If you check the explored app there are a few examples for OData Tree binding and there I'm using the mockserver as well. Maybe that helps...

Nabi
  • 2,536
  • 1
  • 16
  • 18
  • thanks for the reply. I'm not sure if I understand you right. My question is, if there is any way to model JsonObjects inside the manifest.json and is far as i understand you it's not. right? But there is a possibility to do it differently? The problem is, that I'll get the JsonObject over the api, so the mockserver is just for testing. The examples I found in the explored app always use a lokal path to the mock data, but that won't work for me as soon as I'm going to use the api. – vega Mar 15 '18 at 12:21