Skip to Content
1

UI5 Set/Get a Global model

Mar 23, 2017 at 11:06 AM

1.2k

avatar image

I have read a few articles on using a Global model, and, there seem to be a number of ways of doing this, some right and some wrong. I'm avoiding defining my model in manifest at present and opting to define it directly in the component.js. The code I'm using is as follows:

Within the component.js onInit:

//var oModel1 = new sap.ui.model.json.JSONModel("model/Testmodel.json");
var oData = { Test : { name : "World" }	};
var oModel1 = new sap.ui.model.json.JSONModel(oData);
sap.ui.getCore().setModel(oModel1,"oModelTest");

And in my View Controller.js I have the following code within the onInit function:

var oModel = sap.ui.getCore().getModel("oModelTest");

However, oModel is showing as undefined when monitoring in debug. It does not error though. I have also tried using the variable oModel1 as someone mentioned that name should be the same at both ends of the set/get. But alas the same issue.

Someone mentioned that ideally we should not use the core to set/get the global model. I am intrigued to see how I can get this working, and if possible without using the core.

If anyone has this working I would greatly appreciate some demonstration code.

Best regards

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Best Answer
A K Mar 24, 2017 at 11:14 AM
0

use this.getView().setModel(ModelName, "ModelName")

Show 3 Share
10 |10000 characters needed characters left characters exceeded

Yes, I have used that within the component, but, when I then attempt to retrieve the model in the controller using .getModel it does not retrieve anything.

We have also tried to define a model (global) within the manifest, but we seem to hit the same issue that we always hit whenever we attempt to load a json file, in that it fails. Well, it does not fail, but gives a 404 error. We've tried:

oModel1.loadData("model/Testmodel.json");
oModel1.loadData("Testmodel.json");
var oModel1 = new sap.ui.model.json.JSONModel("Testmodel.json";
var oModel1 = new sap.ui.model.json.JSONModel("model/Testmodel.json");

And obviously my json file is in the model subdirectory and called Testmodel.json. I have LINT checked the json file and that's free from error, so it's not that. It's something to with the resources. We are using eclipse as our IDE.

0

why this is the answer????

1

Can you share your code in plunkr.co please?

var oModel1 = new sap.ui.model.json.JSONModel("Testmodel.json"; // Close the bracket




To get model data from view, use this.getView().getModel("MODELNAME") [with the same view-> controller]

outside or other controller-> use this.getView().byId("ID").getModel("ModelName")

0
Gary King Mar 23, 2017 at 12:16 PM
0

I managed to get a solution working, but to be honest I'm not happy about using the core. Here's what I have working.

In the component.js

//Test model

//Test model
var oData = { Test : { name : "World" }	};
var oModel1 = new sap.ui.model.json.JSONModel(oData);
sap.ui.getCore().setModel(oModel1,"oModelTest");
sap.ui.core.UIComponent.prototype.init.apply(this);
//Show in console
console.log(sap.ui.getCore().getModel("oModelTest"));

And in my controller, I have:

var oModel1 = sap.ui.getCore().getModel("oModelTest").getProperty("/Test");

This now works, but as mentioned, I would like to avoid using the core.

Anyone?

Share
10 |10000 characters needed characters left characters exceeded
Jun Wu Mar 23, 2017 at 12:21 PM
0

your code looks ok. but usually we don't use core....

in component.js. this.setModel, this stored model will be available to all view.

Show 3 Share
10 |10000 characters needed characters left characters exceeded

Okay, but what do you think the syntax for the code within the controller should be?.

Obviously this code

 var oModel1 = this.getModel("oModelTest"); 

will not work because this is not pointing at the component.

And I'm pretty sure I can't use the code shown below either, as it does not make sense:

var oModel1 = getView().getModel("oModelTest");

So how does one reference the model set within the component?.

0

just this.getmodel, they will find it.

0

try

this.getOwnerComponent().getModel("oModelTest")
0
Wouter Lemaire
Mar 25, 2017 at 07:57 PM
0

Please don't use "sap.ui.getCore().getModel()". Instead define the model in your manifest. You can access all the models from the manifest in the controller of each view by using "this.getOwnerComponent().getModel()"

In the each controller you can also create additional models with "this.getView().setModel()" and "this.getView().getModel()".

Hope it helps.

Kind regards,

Wouter

Show 2 Share
10 |10000 characters needed characters left characters exceeded

As mentioned, I really did not want to use the core: for creating the global model. We initially had issues loading a json file from the manifest, and as you can see I raised a question about 'why I could not load a json file', but have finally managed to resolve that issue, and so can now load my json model from the manifest as you have suggested. This was what I always had planned to do, just could not initially do it because of loading issues.

My next task is to find out how I can define the model based on metadata from an oData call. I have read somewhere that this can be done, but the article did not go into details. ;)

1

You can also add an odata based model in the manifest.json. It's very easy with the UI editor of the manifest. The component.js will then automatically initialize the model.

Kr,

Wouter

0