function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
smita bhargavasmita bhargava 

application event issue

I am newbie to lightining component and trying to understand applicaiton events.
I am passing value from child component to parent compnent via application event.

Here's the code. 
ApplicationCommunicationEvt
---------------------------

<aura:event type="APPLICATION" 
            description="Event template">
    <aura:attribute name="mesg"
                    type="string"
                    description="Communication Between Multiple Components"
                    access="global"/>

</aura:event>

ChildComponent.cmp
-------------------

<aura:component implements="flexipage:availableForAllPageTypes" 
                access="global">
    <aura:registerEvent type="c:ApplicationCommunicationEvt"
                        name="someName"/>
    
    <aura:attribute name="greeting"
                    type="string"/>
    
    <lightning:input type="string"
                     label="Enter Some Data Here"
                     aura:id="inpData"/>
    
    <lightning:button label="Click Here To Fire The Application Event"
                      onclick="{!c.FireApplicationEvent}"
                      variant="brand"/>
</aura:component>


ChilcComponentController.js
--------------------------

({
    FireApplicationEvent : function(component, event) {
        var message=component.find("inpData").get("v.value");
        alert('message='+message);
        var appEvent=$A.get("e.etaPrime:ApplicationCommunicationEvt");
        appEvent.setParams({"mesg":message});
        appEvent.fire();
    }
})


ParentComponent.cmp
--------------------

<aura:component implements="flexipage:availableForAllPageTypes"
                access="global">
    <!--This component will have an handle(or handle) the event-->
    
    <aura:attribute name="receivedevent"
                    type="string"/>

    <aura:handler event="c:ApplicationCommunicationEvt"
                  action="{!c.HandleTheApplicationEvent}"/>
    
    <!--Now this attribute receivedevent will get the value from the attribute of event-->
    <div class="slds-m-around_xx-small">
        <c:ChildComponent /> 
        <div>
        <p>{!v.receivedevent}</p> 
        </div>
        
    </div>
    
</aura:component>

ParentComponentController.js
-----------------------------

({
	HandleTheApplicationEvent : function(component, event) {
		var evtattribute=event.getParams("mesg");
        alert(evtattribute);
        var response="something is good";
        
        if (evtattribute==='Hi')
        {
            response="Hello";
        }
        
        alert('Now response='+response);
        component.set("v.receivedevent",response);
	}
})
My requirement is if attribute in event is   Hi  then it should show me output as    Hello.

But in parentcomponent controller, the alert(evtattribute) is returning [object object]
so its not taking value in attribute  mesg.

Please help me out.

smita
 
Best Answer chosen by smita bhargava
sfdcMonkey.comsfdcMonkey.com
Hi Smita,
 use ,
 var evtattribute=event.getParam("mesg"); Instead of
 var evtattribute=event.getParams("mesg");

it should be getParam not getParams

Hope it will helps you, let us know if it helps you
thanks sfdcMonkey.com

All Answers

sfdcMonkey.comsfdcMonkey.com
Hi Smita,
 use ,
 var evtattribute=event.getParam("mesg"); Instead of
 var evtattribute=event.getParams("mesg");

it should be getParam not getParams

Hope it will helps you, let us know if it helps you
thanks sfdcMonkey.com
This was selected as the best answer
smita bhargavasmita bhargava
Thanks
I was struggling since the last one hour.