1

I'm new to lightning and this is the first time I'm trying to write a event other than in trailheads. I have two components, one of them is a dynamic picklist and the other component shows a welcome message to the containing the user's name. This should change when a value in the picklist is selected.

Event:

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

Picklist component:

<aura:component controller="NewPicklist" implements="flexipage:availableForAllPageTypes,force:appHostable" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:registerEvent name="NewUser" type="c:NewUser"/>  
<ui:inputSelect label="Viewing As" class="dynamic" aura:id="InputSelectDynamic" value="" change="{!c.onChange}" required="true"/>

Picklist Controller:

 onChange: function (component, event){
    var nam = component.find("InputSelectDynamic").get("v.value");
    //component.find("InputSelectDynamic").set("v.options", nam);
    console.log(nam);
    var eve = $A.get("e.c:NewUser");
    eve.setParams({"newuser":nam});
    eve.fire();
    console.log("event fired!")
}

Welcome message component:

<aura:component controller="Dashboard" implements="flexipage:availableForAllPageTypes,force:appHostable" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:handler event="c:NewUser" name="newuser" action="{!c.getVal}"/>    
<aura:attribute name="Name" type="String"/>    
<h1>Welcome to the Dashboard App: <ui:outputText aura:id="UserName" value=""/></h1>
</aura:component>

Welcome message controller:

getVal: function (component, event){

    console.log("?");
    var showName = event.getParam("newuser");
    console.log(showName);
    component.find("UserName").set("v.value", showName);
}

Any help would be appreciated.

  • 1
    Hi @Nightshade, welcome to SFSE! Could you be more specific about the problem? I see that you put some logs which is very good practice. What are the logs which don't appear? Also, do your two components are inside the same application or in another component? – Martin Lezer Sep 13 '17 at 8:54
  • Hi Martin. Both the components are inside the same application. The logs in the welcome message controller aren't appearing, that is, the getVal function. I want to use the event to change the name in the welcome message. This name should come from the picklist present. – Nightshade Sep 13 '17 at 8:58
  • Ok I think I found the issue. Could you try to change the name of the event handler in your welcome message component to "NewUser"? I think the registered event and the handler must have the same name. – Martin Lezer Sep 13 '17 at 9:00
  • No, it still isn't working. The getVal function is not getting called at all. – Nightshade Sep 13 '17 at 9:02
  • Ok I might have another try. Try to remove the event handler name in your welcome component. – Martin Lezer Sep 13 '17 at 9:04
2

As described in the documentation, application events handler doesn't need any name attribute.

There is no explaination but the documentation says:

The handler for an application event won’t work if you set the name attribute in . Use the name attribute only when you’re handling component events.

Then, in your welcome component, remove the name attribute of your handler:

<aura:handler event="c:NewUser" action="{!c.getVal}"/>  
  • Okay, thank you. – Nightshade Sep 13 '17 at 9:24
  • You're welcome. Glad I could help. – Martin Lezer Sep 13 '17 at 9:25

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.