Skip to Content
0
Nov 11, 2015 at 02:18 AM

How to access model from controller when model is defined in Component.js?

2397 Views

I define a model in Component.js. However, when access it from another JS, it is undefined.

Also tried set model in sap.ui.getCore(), but still it is undefined?!

Codes From Component.js:

createContent : function() {

// create root view

var oView = sap.ui.view({

id : "app",

viewName : "sap.ui.demo.myFiori.view.App",

type : "JS",

viewData : { component : this }

});

var oModel = new sap.ui.model.json.JSONModel({

businessData : [

{SPID:"1",program:"<astc>",speed:29000, process:"ASTC_HANDLER"},

{SPID:"2",program:"SCC_ASE",speed:26900, process:"AWAITING COMMAND"},

{SPID:"3",program:"IMRS",speed:984, process:"IMRS_PACK"},

{SPID:"4",program:"isql",speed:217, process:"AWAITING COMMAND"},

{SPID:"5",program:"LOB_Version_GC_5",speed:11700, process:"LOB GC"}

]

});

oView.setModel(oModel, "cData");

sap.ui.getCore().setModel(oModel, "cData");

-----

Then from another view (Master.controller.js), tried below and get both variables as undefined. I can access them from Master.view.xml though.

onInit: function(oEvent) {

//sap.ui.core.UIComponent.prototype.init.apply(this);

var cdata_view = this.getView().getModel("cData");

var cdata_global = sap.ui.getCore().getModel("cData");

},

How to resolve it?