cancel
Showing results for 
Search instead for 
Did you mean: 

UI5 Set/Get a Global model

gary_king2
Participant

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

Accepted Solutions (1)

Accepted Solutions (1)

karthikarjun
Active Contributor
0 Kudos

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

gary_king2
Participant
0 Kudos

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.

junwu
Active Contributor

why this is the answer????

karthikarjun
Active Contributor
0 Kudos

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")

Answers (3)

Answers (3)

WouterLemaire
Active Contributor

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

gary_king2
Participant

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

WouterLemaire
Active Contributor

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

In this case, how do you access the model from the browser console for debugging purposes? `this` refers to the browser window.

junwu
Active Contributor

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.

gary_king2
Participant
0 Kudos

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

junwu
Active Contributor
0 Kudos

just this.getmodel, they will find it.

former_member227918
Active Contributor

try

this.getOwnerComponent().getModel("oModelTest")
gary_king2
Participant
0 Kudos

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?

Marc1
Explorer
0 Kudos
this.getOwnerComponent().setModel(oModel,"myModel") and this.getOwnerComponent().getModel("myModel")
Marc1
Explorer
0 Kudos
and this.setModel(...) inside the Component.js will set the model on the Component directly. Therefore, the model is visible in all views inside that Component.