lightning components: can helpers call other helpers

  • 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?

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

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM