1

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');
}
 })
  • Check out this Q/A salesforce.stackexchange.com/questions/55459/… – benahm Mar 25 '17 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. – Arpit Sethi Mar 25 '17 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 '17 at 13:36
10

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.

  • 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? – Arpit Sethi Mar 25 '17 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 '17 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.