2

I have created a sample notifier and handler for implementing a application event. The event is firing even when i dont use aura:registerEvent in the notifier.

Then what is the actual use of this tag? In which scenarios do we absolutely need it? Is it just a best practice to use the tag?

Below is a code where i didnt use aura:registerEvent tag:

MyEvent :

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

MyNotifier:

<aura:component>
    <h1>Application Event Sample</h1>
    <p><lightning:button
        label="Click here to fire an application event"
        onclick="{!c.fireApplicationEvent}" />
    </p>
</aura:component>

MyNotifierController:

({
    fireApplicationEvent : function(cmp, event) {
        var appEvent = $A.get("e.c:MyEvent");
        appEvent.setParams({
            "message" : "An application event fired me. "
        });
        appEvent.fire();
    }
})

MyHandler :

<aura:component>
    <aura:attribute name="messageFromEvent" type="String"/>
    <aura:attribute name="numEvents" type="Integer" default="0"/>

    <aura:handler event="c:MyEvent" action="{!c.handleApplicationEvent}"/>

    <p>{!v.messageFromEvent}</p>
    <p>Number of events: {!v.numEvents}</p>
</aura:component>

MyHandlerController:

({
    handleApplicationEvent : function(cmp, event) {
        var message = event.getParam("message");
        cmp.set("v.messageFromEvent", message);
        var numEventsHandled = parseInt(cmp.get("v.numEvents")) + 1;
        cmp.set("v.numEvents", numEventsHandled);
    }
})

MyApplication:

<aura:application >
    <c:MyNotifier/>
    <c:MyHandler/>
</aura:application>
2

From the documentation.

For Application Event:

A component registers that it may fire an application event by using in its markup. The name attribute is required but not used for application events. The name attribute is only relevant for component events. This example uses name="appEvent" but the value isn’t used anywhere.

Syntax for Application Event:

var appEvent = $A.get("e.c:appEvent");
// Optional: set some data for the event (also known as event shape)
// A parameter’s name must match the name attribute
// of one of the event’s <aura:attribute> tags
//appEvent.setParams({ "myParam" : myValue });
appEvent.fire();

For Component Event

To get a reference to a component event in JavaScript, use cmp.getEvent("evtName") where evtName matches the name attribute in .

Use fire() to fire the event from an instance of a component. For example, in an action function in a client-side controller:

Syntax For Component Event:

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

var compEvent = cmp.getEvent("sampleComponentEvent");
// Optional: set some data for the event (also known as event shape)
// A parameter’s name must match the name attribute
// of one of the event’s <aura:attribute> tags
// compEvent.setParams({"myParam" : myValue });
compEvent.fire();

As I can see 2 Points

  1. Component Events are referenced from component using cmp.getEvent("sampleComponentEvent"); the component markup shouldUse Register Event to alias it for future use in Javascript

  2. Applicantion events are referenced globally using $A.get("e.c:appEvent");. As they are present in the whole application thus you can use it without declaring it in markup.

Its not just about custom Application Event, standard salesforce events like refreshView ,e.force:navigateToURL etc can be fired without registering them.

The mention about registerEvent for Application Event is mostly for Code Readability and Make syntax uniform for Application and Component Event.

SRC: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/events_component_fire.htm?search_text=aura:registerEvent

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

| improve this answer | |
1

Best practice is the use the <aura:registerEvent> tag, for a number of reasons:

  1. As @PranayJaiswal mentioned, code readability

  2. If you have complex and multi-layered components, declaring this tag can help with code maintainability in the future.

  3. Registering an event will prevent a deployment that doesn't contain the event component - which is desired when moving code from org to org. It can be frustrating to see an app working in a sandbox/scratch org and then not working in production, all because you forgot to include the appEvent.evt file in the deployment

  4. I've seen inconsistent behavior in lightning components when I don't declare this.

| improve this answer | |

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.