vega
vega

Reputation: 11

Loading JsonObject model in manifest.json - sapui5

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.

Upvotes: 0

Views: 2013

Answers (1)

Nabi
Nabi

Reputation: 2566

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...

Upvotes: 0

Related Questions