Skip to Content
-1

SAP UI5 Problem with Closure

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) {
				}
			});
		},
Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Jul 02 at 01:17 PM

    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){}});
    Add a comment
    10|10000 characters needed characters exceeded

    • 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.

  • Posted on Jul 02 at 02:38 AM

    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)

    Add a comment
    10|10000 characters needed characters exceeded

    • 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

  • Posted on Jul 04 at 05:51 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

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.