Skip to Content

UI5 Set/Get a Global model

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    Mar 24, 2017 at 11:14 AM

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

    Add comment
    10|10000 characters needed characters exceeded

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

  • Mar 23, 2017 at 12:16 PM

    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?

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 23, 2017 at 12:21 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 25, 2017 at 07:57 PM

    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

    Add comment
    10|10000 characters needed characters exceeded