0

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.

4

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();
}})
  • 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 . – Merabti Ibrahim Aug 9 '17 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. – Ajay Prakash Dubey Aug 9 '17 at 11:55
  • Yes, you are right regarding the name of the event in the controller, it seems so obvious. thank you – Merabti Ibrahim Aug 9 '17 at 12:03
  • Glad to resolve the issue. – Ajay Prakash Dubey Aug 9 '17 at 12:06

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.