4

I understand how to code lightning event from developer guide, but still some of part of me puzzling why the event handling needs to be such a lengthy process and declarative instead of some simple code in javascript/jquery,

right now you need to do the following

1. define event component

<aura:event type="COMPONENT">  
    <aura:attribute name="message" type="String"/>  
</aura:event>

2. register event

<aura:registerEvent name="sampleComponentEvent" type="c:compEvent"/>

3. declare event handling at component level or fire in javascript controller

<aura:handler name="sampleComponentEvent" action="{!c.handleSampleEvent}"/>

I see some cleanliness in decoupling events from code which is easy to understand, some re-usability aspects as well, other than what is logic behind this lengthy process of event definition and handling? is event itself considered as component?

2
  • The only reason i can think of is may be event is like a data store or variable .Also keeping attributes in events makes it available to the entire app .But i agree its strange how difficult it can be Mar 3, 2016 at 22:51
  • I agree, compare this to something like socket.io, where you can just broadcast and respond to whatever events you want. Mar 8, 2016 at 20:38

1 Answer 1

2

For general usage of event throwing and handling, I agree it can be a slightly tedious process. But...

Defining the event as a separate component has the advantage that you know the structure of the event being fired. I.e. What needs to be supplied and what is expected to be received in the handlers. Without this we would need to inspect the code to understand what could possibly be included in the event.

From my understanding the reason why registerEvent tag is there is for a couple of reasons:

  1. it allows the documentation to indicate what events could possibly be thrown from the component.
  2. it abstracts away the data type of the component event to the component and gives more clarity to why the event is there. For example, you could have a component event which has several uses and you could define two registered events, such as onchange and onclear.

Lastly, the handler is to redirect these events to the retrospective controller actions. Typically, I use these in the component which is the outer most in the hierarchy. You don't have to, it's just how I use them currently in this scenario. You can find more information on this within the reference manual:

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/events_component_bubbling.htm

You can redirect component events without a handler tag, using attributes on the defined component. Take the ui:inputtext component as an example:

<ui:inputtext change="{!c.handleChangeEvent}" />

Inside the ui:inputtext component there will be a registered event of the name "change" with a type value assigned to it. So without using register event you couldn't use this shortcut and handy feature of Lightning.

Hope this helps!

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.