Thursday 6 February 2020

Difference between component event and Aura method

In simple line aura:methods will be more faster than component event , since it don't required one more communication layer in the form of event. So always use aura:method when ever it is possible .
Here we have taken a example. We have parent component  and child component . both have the aura:attribute vehicles.   We can pass vehicles to child component at the time of Initialization, with out any event . In child component vehicle attribute will get data from parent , doint method will count the sum and assign the value to child attribute count. let us assume that it has 10.
Now in child component we have given possibility to add vehicle. Now in child component java script we have added new vehicle to vehicle attribute and count to count attribute  . At this stage we will have 11 vehicles in vehicle child attribute , count as 11 in child count attribute.  Since the added information was not     communicated back to  parent , parent will have only 10 vehicles in parent vehicle attribute.  To fill this gap we use component events.  Now After component event communication we will have 11 vehicles in both child and parent. Now in parent component we added one more vehicle ,  This time it will automatically communicated with child since we have bind variable at the time of initialization .  Now child will have 12 vehicles in vehicle object but count as 11. At this time component will not initialize again, so do int count will not work. We have option to communicate with  variables, but we don't have option to perform calculations. To fill this gap , we have aura:method in child . when we are adding vehicle from parent    we will execute child component JavaScript method to perform calculations. This is the use of aura:method

No comments:

Post a Comment