cancel
Showing results for 
Search instead for 
Did you mean: 

SAP UI5 Problem with Closure

0 Kudos

Hi,

I have the following code inside a controller that is reading a record using oData model. In success function I am trying to access the outer function variable "that". But I get the error "Uncaught ReferenceError: that is not defined". I am not able to identify the issue. Can you please advise what I am missing? Thanks for the help

Regards, Ravikanth

		onRouteMatched: function () {
			debugger;
			var oModel = this.getOwnerComponent().getModel("PES");
			var path = "/DEFAULTSSet('ABCD')";
			var that = this;
			oModel.read(path, {
				async: false,
				success: function (oData) {
					that.getOwnerComponent().getModel("defaults");
				},
				error: function (oError) {
				}
			});
		},

Accepted Solutions (1)

Accepted Solutions (1)

prashil
Advisor
Advisor

Hi Ravikanth,

Try binding your method with this:

oModel.read(path,{
				async: false,
				success:function(oData){
					this.getOwnerComponent().getModel("defaults");

}.bind(this),
				error:function(oError){}});
0 Kudos

Thanks Prashil. .bind(this) is working but I was trying to work with closure because I can then have access to all other variables in the outer function. For sure we can all the needed variables as part of this variable, but that means crowding this variable.

Answers (2)

Answers (2)

prashil
Advisor
Advisor
0 Kudos

Hi Ravikanth,

I believe most of the variable in the outer function should still be available in your success callback. What is meant is oRouteMatched function variables should also be available except the arguments if used.

For arguments, you can still save this argument in the outer function as local variable and you should be able to access the local variable in the inner function.

Thanks

Prashil

maheshpalavalli
Active Contributor
0 Kudos

Maybe it's because of the onRouteMatched.

When passing the function onRouteMatched, pass like below onRouteMatched.bind(this) or pass 'this' explicitly to function as show below

https://help.sap.com/doc/saphelp_uiaddon20/2.05/en-US/f9/6d2522a5ca4382a274ae3c6d002ca0/frameset.htm

In the same way, u don't need to pass this to that, you can do like below

success: function(){

}.bind(this)

0 Kudos

Hi Mahesh,

Thanks for the input. My onRouteMatched function is called like this:

oPrepareRouter.attachPatternMatched(this.onRouteMatched, this);

As you suggested, I could achieve the access to "this" variable using .bind(this) but as per one of the comment in below blog accessing "this" using closure is faster than binding this onto the method. Hence I am trying to achieve the access using closure.

https://blogs.sap.com/2016/01/26/pattern-to-avoid-var-that-this-with-odata-model-callback-functions/

Regards,

Ravikanth

maheshpalavalli
Active Contributor

asdfsdafgdafgdasdf I am pretty sure passing "this" by bind will not cause any noticeable performance issues.

Coming to your "that" issue, I hope the place you are calling the below code has the proper reference in "this"

var that = this;
oPrepareRouter.attachPatternMatched(this.onRouteMatched, this);

and make sure belore calling the below that "this" has the view reference

oModel.read(path, {

then surely inside the successhandler "that" will be accessible

0 Kudos

Hi Mahesh,

Thanks again for looking into it. At the time of assigning "var that = this" and before "oModel.read" call, this variable has the view reference. Infact oModel itself is created using this variable ("this.getOwnerComponent().getModel("PES");".)

As of now I am going ahead with .bind(this) variant.

Thanks

Ravikanth