cancel
Showing results for 
Search instead for 
Did you mean: 

Getting property value from oJsonModel

Former Member
0 Kudos

Hi, this is my code:


var sServiceUrl = "http://<irrelevantAddress>/sap/opu/odata/sap/<irrelevantServiceName>";

  var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);

  oModel.read("/EmployeeSet('123')", {

    

  success: function(oData, response) {

       oJsonModel.setData(oData);

  },

  failed: function(oData, response) {

       alert("Failed to get Form details from service!");

  }

  });

alert("EmpForename: " + oJsonModel.getProperty("/EmpForename"));

so, I want to get Employee from the oModel by pressing on a button. the oJsonModel.getProperty("/EmpForename") returns the right value after i press the according button the second time. First time i press the button it returns undefined. Why?

Second, i tried using sap.ui.getCore().getModel(). But what if i have many models. I tried different approaches from scn and other places for setting id or names for the models but i always get undefined. That is why I just wanted to use oJsonModel and get values from there but that didnt give me much aswell.

So I would like to know why is oJsonModel.getProperty("/propertyName") doesnt return the right value the first time i try to set oData, or what is the easiest way to use models by their names or ids (would be cool if without the binding thingy because im not familiar with bindings).

Sorry for messy English and thanks

Accepted Solutions (1)

Accepted Solutions (1)

former_member182862
Active Contributor
0 Kudos

Can it be because the call to the server is async?

that's the success function is called after your alert function is called.

-D

Answers (1)

Answers (1)

santhu_gowdaz
Active Contributor
0 Kudos

oModel.read("/EmployeeSet('123')", { 

      

  success: function(oData, response) { 

       oJsonModel.setData(oData); 

       alert("EmpForename: " + oJsonModel.getProperty("/EmpForename"));

  }, 

  failed: function(oData, response) { 

       alert("Failed to get Form details from service!"); 

  } 

  }); 

Now you will get correct data.

---------------------------------------------------------------------------------------------------------

And for multimodel, create a reference of that model. through that reference you will get the details.so multiple models have multiple types of records, using the reference model name you can access correct values on that model you can do like this,

oModel.read("/EmployeeSet('123')", { 

      

  success: function(oData, response) { 

       oJsonModel.setData(oData); 

       var oModel = new sap.ui.model.json.JSONModel(oJsonModel);

        this.getView().setModel(oModel, "HeaderModel");

   

  }, 

  failed: function(oData, response) { 

       alert("Failed to get Form details from service!"); 

  } 

  });

  If you want to display those value in your view,

  <Text text="{HeaderModel>/EmpForename}" />