Sunday, September 17, 2017

Application Events vs Component Events : Which/How to use for handling custom events in Lightning

Lightning framework is based on event-driven architecture which allows to communicate between different events. Lightning events can be fired or handled by javascript controller. Event are triggered by user action.

As we know that along with system events, there are 2 types of custom lightning events:
  • Application Events
  • Component Events

Here I am going to explain difference between them along with code sample.

Component Events 
  1. Components events can be handled by same component or component which is present in containment hierarchy (component that instantiates or contain component).
  2. Below is syntax for creating component event.

     <aura:event type="COMPONENT" description="Event template" >                                          <aura:attribute name="msg" type="String" access="GLOBAL"/>                                               </aura:event>

  3. Below is syntax for firing component events from javascript controller.

    var accidentEvent = component.getEvent("newCarAccident"); accidentEvent.setParams({"msg":"New Message!!!."});                               accidentEvent.fire();

  4. While handling component events, we need to specify name attribute in  <aura:handler>

    <aura:handler action="{!c.handleNotification}" event="c:carAccidentComponentEvent" name="newCarAccident">

    Make sure that name attribute is same as that of name attribute while registering the event.


Application Events

  1. This kind of events can be handled by any component which is listening to it (have handler defined for event). It is kind of publish-subscribe modal.
  2. Below is syntax of creating application event.

    <aura:event type="APPLICATION" description="Event template" >                                                     <aura:attribute name="msg" type="String" access="GLOBAL"/>                           </aura:event>


  3. Below is syntax to file application events from javascript controller.

    var appEvent = $A.get("e.c:carAccidentAppEvent");               appEvent.setParams({"msg":"New Message!!!."});                                               appEvent.fire();

  4. While handling application events, no need to specify name attribute in <aura:handler>

    <aura:handler event="c:carAccidentAppEvent" action="{!c.handleNotification}"/>
System Events
  1. These events are fired automatically by the framework such as during component initialization, attribute value change, rendering etc. All Components can register for system events in their HTML markup.
  2. Few examples of system events are init, aura:waiting, aura:doneWaiting, aura:doneRendering etc.
  3. If app or component is rerendered, then init event is fired. If server side call is made in init then, aura:waiting event is fired. Once the server call is finished then aura:doneWaiting event is fired. After this aura:doneRendering is fired.
Events Best Practices:
  1. Always try to use component events. Component event usage is more localized as these can be used by same component or component in containment hierarchy.
  2. If you use application events then it may fire system events.
  3. Use Application events only if components are present in 2 different app or are not in containment hierarchy.

I have created GitHub project which contains example demo for Application events and Component Events. You can download complete code from below URL:


Hope this help!!!



More Blogs>>: 
LIGHTNING COMPONENT TO DISPLAY SETUP AUDIT TRAIL DATA    
AURA:IF vs SLDS-SHOW/HIDE    
INHERITANCE IN LIGHTNING    
FIRING EVENT FROM LIGHTNING COMPONENT AND PASSING IT TO VF PAGE    
CHANGES TO LIGHTNING DATA SERVICE IN SUMMER'17    
LIGHTNING DATA SERVICES    
PASSING LIGHTNING COMPONENT ATTRIBUTE VALUE FROM VF PAGE    
FIRE LIGHTNING EVENTS FROM VF PAGE    
DYNAMICALLY CREATING AND DESTROYING LIGHTNING COMPONENTS    
RAISING AND HANDLING CUSTOM EVENTS IN sALESFORCE lIGHTNING    
WHY TO USE DESIGN RESOURCE AND HOW TO ADD DYNAMIC OPTION TO DATASOURCE    
PASSING LIGHTNING COMPONENT ATTRIBUTE VALUE FROM VF PAGE    
PASSING INNER WRAPPER CLASS TO LIGHTNING COMPONENT    
LIGHTNING COMPONENT FOR RECORDTYPE SELECTION FOR ANY SOBJECT    
CUSTOM COMPONENT TO SHOW/HIDE SPINNER IMAGE    

58 comments:

  1. I do recommend anyone interested in Event photography going on the excellent training course supplied by Systems Insight, speak to Mike Orr, Stuart or Darren at systems insight. I've also found the Event Photographers Society really useful.
    party tent hire Doncaster

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This guide to Event photography is based on my personal experience and you are welcome to reproduce it as long as you credit this article with the website address
    paintglow

    ReplyDelete
  4. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. Tenth Floor Events

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Research suppliers and convey your prerequisites, working intimately with them to guarantee right measures of assets are accessible, so things run easily. http://eddyleisure.com/stage-hire-yorkshire/

    ReplyDelete
  7. Good to know about this informative blog. Thanks for sharing this blog. Add a romantic touch to your next event with our dimmable LED fairy lights. If you know more about the offers go through fairy light hire melbourne


    ReplyDelete
  8. They didn't observe Christmas. Why? To find that answer, we should take a gander at the historical backdrop of this occasion which really originates before the introduction of Jesus. Brian May

    ReplyDelete
  9. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. Event Videographer Dubai

    ReplyDelete
  10. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! Latest & Bugs Free Version

    ReplyDelete
  11. I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up.. TubeMate YouTube Downloader For Android Smartphone

    ReplyDelete
  12. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own Blog Engine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it. Download Tubidy APK for Android Free Latest Version

    ReplyDelete
  13. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! i'm Faria Alam

    ReplyDelete
  14. America Gun Ban Home, Syndicated by Countrymen Your News Source Worldwide. Taking the stink out of Gun Control and being a Patriot.  forro festival

    ReplyDelete
  15. Lighting is definitely one of the most important aspects of an event which give extra ordinary look to your event. Get Light and Sound Rental services from d24ksound.com. Visit us on Light and Sound Rental near Me

    ReplyDelete
  16. Concrete mortar was utilized to make sure about each square in the development and numerous pyramids are as yet standing today. Chape

    ReplyDelete
  17. I’m satisfied with the information that you provide for me and thanks for this because sometimes people face this problem. we provide Audio Visual Hire Melbourne at affordable prices. for more info visit our website.

    ReplyDelete
  18. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. applicant tracking software

    ReplyDelete
  19. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. black satta king

    ReplyDelete
  20. Great article Lot's of information to Read...Great Man Keep Posting and update to People..Thanks thc oil for sale

    ReplyDelete
  21. I found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information. THC Vape discreet shipping

    ReplyDelete
  22. You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! cheap thc carts

    ReplyDelete
  23. I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best! [url=https://www.piccosalesbuds.com/cartridges/]thc carts for sale bulk[/url]

    ReplyDelete
  24. Rapidly this website may irrefutably end up being well-known amongst just about all running a blog individuals, due to the diligent content articles or even evaluations. real weed for sale

    ReplyDelete
  25. There are plenty of dissertation web pages internet as you're get hold of of course revealed in the web-site. online dispensary shipping usa

    ReplyDelete
  26. Yes, I am entirely agreed with this article, and I just want say that this article is very helpful and enlightening. I also have some precious piece of concerned info !!!!!!Thanks. Ципрофлоксацин

    ReplyDelete
  27. This is actually the kind of information I have been trying to find. Thank you for writing this information. slsocialmedia.com

    ReplyDelete
  28. Research suppliers and convey your prerequisites, working intimately with them to guarantee right measures of assets are accessible, so things run easily.
    how changes are handled

    ReplyDelete
  29. Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. legit Miami Audi Dealer

    ReplyDelete
  30. I think that thanks for the valuabe information and insights you have so provided here. Marijuana for sale online

    ReplyDelete
  31. The post is written in very a good manner and it contains many useful information for me. Online Dispensary

    ReplyDelete
  32. Very nice article, I enjoyed reading your post, very nice share, I want to twit this to my followers. Thanks!. buy thc vape juice

    ReplyDelete
  33. Great things you’ve always shared with us. Just keep writing this kind of posts.The time which was wasted in traveling for tuition now it can be used for studies.Thanks buy real weed online

    ReplyDelete
  34. Great knowledge, do anyone mind merely reference back to it buy real weed online

    ReplyDelete
  35. Wonderful blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it. thc carts for sale bulk

    ReplyDelete
  36. This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. birra artigianale online

    ReplyDelete
  37. This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. scarpe versace jeans couture

    ReplyDelete
  38. Great post, and great website. Thanks for the information! situs slot online

    ReplyDelete
  39. I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Thanks for sharing. bahis siteleri listesi

    ReplyDelete
  40. Please continue this great work and I look forward to more of your awesome blog posts. black satta king

    ReplyDelete
  41. Applying to colleges or universities is an exciting milestone for many high school graduates; yet far too often, the Fókuszpályázat process itself becomes frustrating and intimidating. Faced with a myriad of information via booklets, forms and well-meaning but often misguided parents and peers, things can become a bit overwhelming.

    ReplyDelete
  42. Many business organizations fail to identify their business goals and hence end up wasting a lot of money by hiring services of wrong web designing company. This article encourages website owners to plan web designing according to their business needs so that they can hire the services of best web designing company. Website Trojans, Malwares and Virus Removal Services

    ReplyDelete
  43. The Android Vs Apple debate has been raging on for a long time now and it was never this big. For long Apple had been the reigning king of smartphones. But, that was certainly pinching Google. So, as we all know, led to the evolution of Android. The mighty OS which has risen in popularity far surpassing many of the big fish and proving to be a strong competitor to Apple and has resulted in a virtual war of Android Vs Apple. Genuine app for Sweatcoin

    ReplyDelete
  44. And if you are not treating any of your conditions or ailments with it, it won't be termed as medical marijuana. It is widely used all over the world under legal framework for various medical purposes. buy weed online

    ReplyDelete
  45. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.

    mixing and mastering tips

    ReplyDelete
  46. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. Ethereum Classic

    ReplyDelete
  47. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. buy ecurrency verified account

    ReplyDelete
  48. This comment has been removed by the author.

    ReplyDelete
  49. This comment has been removed by the author.

    ReplyDelete
  50. This comment has been removed by the author.

    ReplyDelete
  51. Shop the latest collection of ladies jackets in pakistan at Nayza. Find trendy and stylish designs, including a variety of jacket styles and designs, all available in Pakistan.

    ReplyDelete
  52. Are you looking for the latest velvet dress in Pakistan at a low price? Buy the latest velvet dresses online at Jazmin in all over Pakistan with fast delivery.

    ReplyDelete