9

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.

  • You can try using component event which will bubble to the containment hierarchy . – Mohith Shrivastava Oct 31 '16 at 15:12
12

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.

  • That's great thanks! I'll give that a go in my use case. Didn't think to try that given it's not a server side action, but if it works, it works. I wonder if it is an officially supported solution? I tried to find the initial documentation that led me to the initial design pattern (was on a trailhead tutorial) but it seems to have been removed, making me think that it actually isn't supported. – Chris Woolcott Nov 1 '16 at 15:41
  • 1
    It's actually official... $A.enqueueAction(clickme) is the documented way of doing this. developer.salesforce.com/docs/atlas.en-us.lightning.meta/… – Andres Perez Nov 9 '16 at 21:28
  • 1
    Any ideas on how to add a parameter to the action and pull it out on the other side? I tried something like this: "clickme.setParam('context', context);" and then trying to get the parameter from the event object. There's no error, but nothing comes through. The event object is undefined. – Joe B Dec 11 '16 at 21:50
  • @JoeB Same question here. From my experiments it looks like that action is just immutable, i.e. thing in itself - all modifications would be silently ignored. The only thing you could do with it, just execute as it is) Any updates/workarounds from your side? – wesaw Jun 17 '17 at 13:32
  • Thanks! This solved a question i was looking to answer +1 from me – Ronnie Aug 12 '18 at 22:22
4

As of API v41.0, Salesforce discourages calling Aura.Action directly and recommends using Events as the standard way to communicate between components.

enter image description here

  • I don't know why SF promotes that archtecture. Firing random events between components without central store to keep state (aka Redux) is known to turn into a spaghetti over time as events can be fired and received from basically anywhere (aka backbone mvc type). Then I rather prefer to pass callbacks to children where there is clear trace (pretty much like Android / React)... – Trace Mar 29 at 12:02

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.