21

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?

0

1 Answer 1

29

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;
    }
})
0

You must log in to answer this question.

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