Tips and Tricks for Lightning events

Manjot Singh
Salesforce-Lightning
2 min readMar 30, 2018

Event-driven programming is used in many languages and frameworks. The idea is that you write handlers that respond to interface events as they occur. So there is some helpful techniques we can use while using Events in Salesforce Lightning.

  • Get Event source and get/set source attributes

When you are firing an event, you can send data in that event. But there is no need to send public and global attributes in a event. You can actually access those values directly where event is handled. Lets say you fired an event from ComponentA and it is handled in ComponentB. ComponentA has attributes attribute1 and attribute2. I can get these attributes using event.getSource().get(<attributeName>).

we can get/set only public and global attributes of event source.

  • Add data to event while propagating

We can add data while event is propagating from one component to another.

We fired a event from CompA. But this event doesnot have all the required information needed at CompC. That information is at CompB. So we have to add that information when component propagate through CompB. We will use Bubble Phase in handlers of Component.

when this event is handled in CompC then we can get data that was passed from CompA and also from CompB.

  • Firing Event after Init

If you want to fire a event when init of all component is done. You can do so by writing

setTimeout($A.getCallback(() => component.getEvent('add')
.setParams({actionobject:''}).fire()
));

If you have any other tricks you are using please share in comments.

Reference :- https://salesforce.stackexchange.com/questions/208737/can-parent-handle-childs-event-fired-by-child-during-init

--

--