cancel
Showing results for 
Search instead for 
Did you mean: 

Call Function from extended Controller

gopi_nidjelli2
Participant
0 Kudos

Hi All,

       I need to make some changes to the standard app. I have extended the controller to implement the custom logic. how to call the parent function from the extended controller?

Ex: in S1.Controller.js I have a function senddata and checkdata(). I have extended the controller as S1Custom.Controller.js. In the senddata method i have to perform some additional validations but Can I use the checkdata() function from the parent?

Thanks

Gopi

Accepted Solutions (1)

Accepted Solutions (1)

D_Chia
Active Participant
0 Kudos

you used sap.ui.define and Controller.extend ("Custom.Controller", ...) ?

actually, if you did not "re-define" checkdata method in Custom.Controller (to override the base's logic) ... you can just use this.checkdata() too. Javascript will search up the prototype chain and execute from the parent if it finds the method there.

or you can explicitly call the method by referring to the parent's prototype like below:-

Controller.prototype.checkdata.call(this);   // you can pass arguments too

gopi_nidjelli2
Participant
0 Kudos

thank you. it worked.

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

gopi_nidjelli2
Participant
0 Kudos

Hi,

  Here's the solution.

A Confirmation dialog was raised from the S1Custom.Controller.js. When User Click Accept button, I need to call a method checkData(). But when I used this.checkData(), it was throwing an error.

So before the Dialog was raised I copied the reference from this to that.

ex: var that = this; and called the that.checkData();

Thanks

Gopi

D_Chia
Active Participant

thanks for the update and glad things are working now. 

anyhow, for the sake of clarity and completeness - for others reading (who may not be familiar with Javascript) - the root cause of what you have shared in the latest post is different from your initial question at the start of this thread.

essentially it was a scope issue with "this". because you were expecting "this" to be the Custom Controller but instead it was the Dialog's

this.checkData() would have worked if "this" was the Custom Controller. few ways that this will be the case:-

  1. implicitly due to call being made within the Custom Controller context
  2. if you performed a bind/apply to Custom Controller explicitly before the call is done
  3. like your solution which was to use a "saved" reference to the Custom Controller instance

Answers (0)