Although not documented very well, you can pass an Aura.Action (essentially a javascript function) as an attribute in a Lightning Component.
For example, the following works.
Parent Component
<aura:component>
<c:childComponent clickme="{!c.click}"/>
</aura:component>
Parent Controller
({
click:function(cmp, event, helper){
console.log('click!');
}
})
Child Component
<aura:component>
<aura:attribute name="clickme" type="Aura.Action"/>
<a onclick="{!v.clickme}">Click Me!</a>
</aura:component>
Clicking the link the fires the action passed in from the parent and the string is logged in the console.
My question is, can I fire the same function passed in from the Child Controller?
Something like:
Child Controller
({
myFunction: function(cmp, event, helper){
var clickme = cmp.get('v.clickme');
clickme.fire();
}
})
I'm sure it is possible, just not sure on the syntax. Any pointers will be greatly appreciated.
Best Answer
I have not found this documented anywhere (just like you said in your original post), but I have found that you can accomplish this by using
$A.enqueueAction(clickme)
. This also seems to fire immediately because there is no server connection involved so the framework must see that it does not actually need to go in the server queue.