1

I am having problems firing an application Event, i do not know what i am messing. Here is my code: APP:

<aura:application extends="force:slds">
    <c:QuizWelcomeComponent />
    <c:QuizQuestionComponent /> 
</aura:application>

QuizWelcomeComponent: (should fire an event to QuizQuestionComponent component)

<aura:component implements="flexipage:availableForAllPageTypes" access="global">
    <aura:registerEvent name="StartQuiz" type="c:StartQuizEvent"/>
        ......
        ......
    <button class="slds-button slds-button_brand" onclick="{!c.StartQuiz}">Commencer le Quiz</button>
</aura:component>

controller:

StartQuiz : function(component, event, helper) {
        var StartEvent =  $A.get("e.c:StartQuiz");
        console.log('StartEvent = '+StartEvent);
        StartEvent.fire();

}

StartQuizEvent:

<aura:event type="APPLICATION">

</aura:event>

ERROR: console.log('StartEvent = '+StartEvent) from the QuizWelcomeComponent component returns undefined so I do not know what I am doing wrong, I even copied the firing code from the APPLICATION EVENT DOCs.

1 Answer 1

5

You should define the name of the application event in javascript controller same as the application event name.

<aura:component implements="flexipage:availableForAllPageTypes" access="global">
<aura:registerEvent name="StartQuiz" type="c:StartQuizEvent"/>
<button class="slds-button slds-button_brand" onclick="{!c.StartQuiz}">Commencer le Quiz</button>

Controller-

({
StartQuiz : function(component, event, helper) {
    var StartEvent =  $A.get("e.c:StartQuizEvent");
    console.log('StartEvent = '+StartEvent);
    StartEvent.fire();
}})
5
  • No i dont think so, The name of the registerEvent does not have to be the same as the name of the event itself, you can refer to this article developer.salesforce.com/docs/atlas.en-us.lightning.meta/…. this is just an example .
    – user28829
    Aug 9, 2017 at 11:43
  • @MerabtiIbrahim In the application event it is necessary to take name of the application event in javaccript controller like $A.get("e.c:StartQuizEvent"); I have updated the Answer. Aug 9, 2017 at 11:55
  • Yes, you are right regarding the name of the event in the controller, it seems so obvious. thank you
    – user28829
    Aug 9, 2017 at 12:03
  • 1
    Glad to resolve the issue. Aug 9, 2017 at 12:06
  • After a long struggle, It worked.... thank you...
    – Kamal
    Feb 5, 2020 at 6:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .