Skip to Content
-1

SAPUI5: getModel returns undefined if called within the same function of setModel

I'm trying to set a model and retrieving it from OData after pressing a certain button.

The problem is when I call getModel outside of the "success" function, it returns undefined.

Because "onPressButton1" runs fully before the "success" function gets executed.

So is there a way to make sure "success" function is called first? and within the onPressButton1.

If I put my whole code in "success" function, I'll not be able to use "this" key word to call functions "this.fDoThis();"

Code for reference:

onPressButton1: function() {
	var vEntityURL = "/CustomerSet(ID='000')";
	var sServiceUrl = "/Customers_SRV/";
	var oServiceModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);
	var oJsonModel = new sap.ui.model.json.JSONModel();

	oServiceModel.read(vEntityURL, {
		success: function(oData) {
		oJsonModel.setData(oData);
		sap.ui.getCore().setModel(oJsonModel, "Customers");
	}
});

	var oCustomer = this.getView().getModel("Customers");
	console.log(oCustomer.getProperty("/Name"));
					}

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on Jul 21, 2020 at 11:04 AM

    Use V2 odata model, v1 is deprecated.

    Try chanfing the success handler as shown below. Add.bind(this) and set model to view

    success: function(oData) {
    		oJsonModel.setData(oData);
    		this.getView().setModel(oJsonModel, "Customers");
    	}.bin(this)
    Add a comment
    10|10000 characters needed characters exceeded

    • Thanks now I'm able to use "this". However, any chance in making the success runs before its parent function?

      for example the following returns an error in console unless button is pressed twice:

      onPressButton1:function(){
      	var vEntityURL ="/CustomerSet(ID='000')";
      	var sServiceUrl ="/Customers_SRV/";
      	var oServiceModel =newsap.ui.model.odata.ODataModel(sServiceUrl, true);
      	var oJsonModel =newsap.ui.model.json.JSONModel();
      
      	oServiceModel.read(vEntityURL,{
      		success:function(oData){
      		oJsonModel.setData(oData);
                      this.getView().setModel(oJsonModel,"Customers");
      
      }.bind(this)
      
      });
      
      	var oCustomer = this.getView().getModel("Customers");
      	console.log(oCustomer.getProperty("/Name"));}

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.