Skip to Content
-1
Jul 21, 2020 at 10:40 AM

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

537 Views

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"));
					}