Friday, March 3, 2017

Raising and Handling Custom Events in Salesforce Lightning

Events in Lightning plays very important role when we have to communicate between different components. Suppose a component is present inside another component and we want to change behavior of parent component based on actions on inner/child components.

I will try to explain this concept with real life example.

Suppose you are driving and suddenly you witness an accident and you need to notify the nearest hospital about this accident so that they can send ambulance for help.



In this scenario, car accident is an event which happened. You are notifier who witness this event (register this event). After witnessing this event, you are going to fire an event through which you will send some message to near by hospital (other component) so that it will perform some action by sending ambulance for help.

Now if we need to understand in Lightning terms, then we create different events which will be used by components to interact between them. Below are different steps for this event handling:
  • Create a Lightning event and create different attributes in it which can be used to pass information between components.
  • Component which want to fire an event needs to first register for an event by using <aura:registerEvent>. Then only it can fire an event and other component can receive it.
  • Component which wants to receive information will handle the event using <aura:handler> and type as Ligthning Event which we create.
Below image will help you to understand custom events using lightning code:



Below is complete code:


If you preview your app, then you will see below output:


Hope this will help you to understand the custom event handling...


More Blogs>>: 
DYNAMICALLY CREATING AND DESTROYING LIGHTNING COMPONENTS    
BOX AND SALESFORCE INTEGRATION    
INTEGRATING BOX FILE PICKER WITH SALESFORCE    
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IT IN SALESFORCE    

7 comments:

  1. It may be as a bank advance, money from the business, renting, charge card, the trusty 'Bank of Mum and Father', or horde different types of fund, however moderately scarcely any individuals really purchase a vehicle with their own money any longer.MOTORSCHADEN ANKAUF DARMSTADT

    ReplyDelete
  2. Gosh that's so many steps LWC has simplified approach revisiting lightning after a while do surprise me looks ancient.

    ReplyDelete
  3. Gosh that's so many steps LWC has simplified approach revisiting lightning after a while do surprise me looks ancient.

    ReplyDelete
  4. While renting a luxury car, remember that it is often less expensive to rent a car for a longer period, say a week, than shorter period. So if you can afford to have an extended vacation or business trip, rent out the car for longer days. Luxury Car Rental Miami

    ReplyDelete