cancel
Showing results for 
Search instead for 
Did you mean: 

How to set entities via global JSON model

Calcifer
Explorer
0 Kudos
186

Hi experts,

I am new to SAPUI5 and still learning.
I have this problem where the skills and proficiency values should come from the entities via a global JSON Model. I have tried everything and searched everywhere but it is not working.

I decided to put my global json model in COMPONENT.JS

 

sap.ui.define ([
 "sap/ui/core/UIComponent"
 "sap/ui/model/json/JSONModel"
], function (UIComponent, JSONModel) {
   return UI.Component.extend("namespace",{
   //define the global JSON model
   var oModel = new JSONModel();
   this.setModel(oModel, "globalModel");

  //Load data
  var oData = {
      skills: [
          {id: 1, skill: "JavaScript"},
          {id: 2, skill: "SAPUI5:}
  ]
  proficiencies: [
          {id: 1, level: "Beginner"},
          {id: 2, level: "Expert"}
  ]
};
 oModel.setData(oData)
}
});

 

In my mainview.xml

 

<VBox>
 <ComboBox items="{path: 'globalModel>/skills'}"
  placeholder="Select Skill">
  <core:Item key="{globalModel>id}" text="{globalName>skill}"/>
 </ComboBox>
 <ComboBox items="{path: 'globalModel>/proficiencies'}"
  placeholder="Select Proficiency">
  <core:Item kety="{globalModel>id}" text="{globalName>level}"/>
 </ComboBox>
  

 

In my controller.js

 

return Controller.extend("namespace"){
  onInit: function (){
  //Access Global
  var oModel = this.getOwnerComponent().getModel("globalModel);
  console.log(oModel.getData());
 }
}

 

Appreciate you help!

View Entire Topic
junwu
Active Contributor
0 Kudos
sap.ui.define ([
 "sap/ui/core/UIComponent"
 "sap/ui/model/json/JSONModel"
], function (UIComponent, JSONModel) {
   return UI.Component.extend("namespace",{
   //define the global JSON model
   var oModel = new JSONModel();

  //Load data
  var oData = {
      skills: [
          {id: 1, skill: "JavaScript"},
          {id: 2, skill: "SAPUI5:}
  ]
  proficiencies: [
          {id: 1, level: "Beginner"},
          {id: 2, level: "Expert"}
  ]
};
 oModel.setData(oData);
this.setModel(oModel,"globalModel")
}
});
Calcifer
Explorer
0 Kudos
Hi junwu, i edited my post. I included the this.setModel(oModel,"globalModel") as I have forgotten it. Still not working for me.
junwu
Active Contributor
0 Kudos
how your view is created at runtime? is it created by routing?
Calcifer
Explorer
0 Kudos
hi Junwu, yes i did the routing and navigation. Right now I can only display but when I try to add the global JSON Model, it will now have an error.