Saturday 19 December 2020

Lightning components: can helpers call other helpers

You can definitely call another function within the same object using the this keyword.  Below Example doesnt make sense but it demonstrates the functionality of code

Lightning component : 

<aura:component >

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

</aura:component>

Controller :

({

    buttonClicked : function(component, event, helper) {

        helper.callHelper(component, event.target.id);

    }

})

Helper:

 ({

    callHelper : function(component, buttonId) 

    {

        console.log('call1',buttonId);

     this.helper1(component, buttonId,'Test');  

    },

    helper1 : function(component, buttonId, newLabel)

    {

        console.log('call2',buttonId);

       console.log('call3',newLabel);

    }

})