20

I like to compose functions from smaller blocks.

helperA : function (component, param){
  //do stuff
}

helperB : function (component, params){
  //do stuff, and
  helperA(component, params)
}

That doesn't work. Also tried

  • sticking this. in front of it
  • passing the function helperA in as a param to helperB so that it's defined.

How do I call a function from another, either in the controller or the helper?

24

You can definitely call another function within the same object using the this keyword. I just tested the following code and it works. The example doesn't make sense but it demonstrates the functionality of the code:

Component:

<aura:component >
    <input type="button" onclick="{!c.buttonClicked}" value="Click Me" id="myButton" />
</aura:component>

Controller:

({
    buttonClicked : function(component, event, helper) {
        helper.changeMyLabel(component, event.target.id);
    }
})

Helper:

({
    changeMyLabel : function(component, buttonId) 
    {
        var newLabel = 'My Label Changed';
        this.changeButtonLabel(component, buttonId, newLabel);  
    },
    changeButtonLabel : function(component, buttonId, newLabel)
    {
        document.getElementById(buttonId).value = newLabel;
    }
})

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.