3

Is there any way to do call method1 from method2 which are in the same controller. I tried to do that with the "this" keyword but it does not seems to work. I know that common code should be kept in the helper method but want to try this stuff.

({
bar : function(component, event, helper) {
       this.foo(); // it does not work. Error:- Cannot read property 'foo' of undefined]]
},
foo : function() {
     console.log('foo called');
}
 })
3
  • Check out this Q/A salesforce.stackexchange.com/questions/55459/…
    – benahm
    Mar 25, 2017 at 11:58
  • Thanks Benahm. The solution which has been provided there is:- var action = component.get("c.bar"); action.$meth$(); can you please explain action.$meth$(); I did not get it. Mar 25, 2017 at 12:48
  • from the name you can see that it's an internal function to the Aura framework, so follow the framework and use the helper....
    – benahm
    Mar 25, 2017 at 13:36

2 Answers 2

13

Short answer is No. Methods in the same controller cannot talk to each other because this will always be undefined in the controller. You need to use the helper for this case. Look at the answer by Doug.

Although there's a way this can be done using aura:method, see here. I would strongly recommend you to use helper instead of aura:method.

2
  • Thanks Praveen for the detailed explanation. As you said "Methods in the same controller cannot talk to each other because this will always be undefined in the controller". Can you please explain why it will be always "undefined", is there any specific reason for this kind of behavior? Mar 25, 2017 at 13:54
  • 2
    I believe this due the way in which the methods are wrapped in controller.js under strict mode.
    – Praveen
    Mar 25, 2017 at 14:15
0

Yes, We can call the controller method inSide controller. See below code and try.

({
    handleButtonClick : function(component, event, helper) {
      console.log('handleButtonClick called !!!!!!');
      let secondAction = component.get('c.hanndleButtonClickSecond');
      $A.enqueueAction(secondAction);  
    },
    
    hanndleButtonClickSecond: function (cmp,event,helper){
        alert('handleButtonClickSecond Called successfully!!!!!!!!');
    }
})

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .