Skip to Content

SAP UI5 Problem with Closure


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 () {
			var oModel = this.getOwnerComponent().getModel("PES");
			var path = "/DEFAULTSSet('ABCD')";
			var that = this;, {
				async: false,
				success: function (oData) {
				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:,{
    				async: false,
    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

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

    success: function(){


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



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



    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.