cancel
Showing results for 
Search instead for 
Did you mean: 

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

abdulelahzh
Explorer
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor

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)

Answers (0)