cancel
Showing results for 
Search instead for 
Did you mean: 

How to call a function from one function in sapui5

0 Kudos
4,541

Hi experts ,

I developed here one oData Get call  that is working fine. But form the success function i just need to call one more function there .

So how to call the other function from the success function.

Plz correct me if i may did wrong anywhere. And send me your guidelines.

I attached my coding here .

press :function () {

  var surl = "service url here ";

     OData.request

                      ({ 

        requestUri:surl, 

                 method: "GET", 

                 headers:

                     {      

       "X-Requested-With": "XMLHttpRequest",

       "Content-Type": "application/atom+xml",

       "DataServiceVersion": "2.0",         

      "X-CSRF-Token":"Fetch"                               

                }       

              },

             function(data, response) { // onSuccess function called when everything is ok

  

       this.applySearch(); // if i call  the function like this i am getting error like this is not a function like that

   

   

    }

},

applySearh : function  () {

}

Thanks and  Regards ,

Damian .A

Accepted Solutions (1)

Accepted Solutions (1)

SergioG_TX
Active Contributor

Damian,

that is because this is out of scope... you can have

var self = this;

right before your ajax call..

then inside the success function:

self.applySearch();  // instead of this

0 Kudos

Thank you so much  i got the

answer ..

agentry_src
Active Contributor
0 Kudos

Please mark this Discussion with a Correct Answer (closes, but does not lock the Discussion) and Helpful Answer where appropriate. See http://scn.sap.com/community/support/blog/2013/04/03/how-to-close-a-discussion-and-why   Even if you discovered the solution without any outside contributions, it helps others to understand what the solution turned out to be. 

Do not use Assumed Answered as it confuses anyone looking for the specific answer.  If you dig into the Getting Started link (top right of each SCN page), you are only instructed to mark Helpful or Correct Answers to Discussion responses. 

Thanks, Mike (Moderator)

SAP Technology RIG

Answers (2)

Answers (2)

jamie_cawley
Product and Topic Expert
Product and Topic Expert
0 Kudos

You could set a var to the context of press

var self =  this;

and then in you success function

self.applySearch()

There's also other ways such as jQuery.proxy to do the same.

Regards,

Jamie

SAP - Technology RIG

Chantele
Active Participant
0 Kudos

When you call a function from inside another you need to refer to it with the "this" keyword.

this refers to the javascript file you are inside of.

In your instance you are on the third level.

So you need to set "this inside your main fuction as something else.

Take a look below this is how your code will change (the bold parts)

press :function () {

    that = this;

  var surl = "service url here ";

     OData.request

                      ({

        requestUri:surl,

                 method: "GET",

                 headers:

                     {     

       "X-Requested-With": "XMLHttpRequest",

       "Content-Type": "application/atom+xml",

       "DataServiceVersion": "2.0",        

      "X-CSRF-Token":"Fetch"                              

                }      

              },

             function(data, response) { // onSuccess function called when everything is ok

 

       that.applySearch(); // if i call  the function like this i am getting error like this is not a function like that

  

  

    }

},

applySearh : function  () {

}