1

I just started lightning component development. When i tried to fire an event from the page its not firing. Can somebody suggest me where i missed it. Here is my code. I just like to know where i missed it. I tried in all the possible ways but no luck. Can somebody tell me where i am missing. Thanks in advance.

Component:

<aura:component >
    <aura:attribute name="acc" type="Account" default="{sobjectType:'Account',
                                                       name:'',
                                                       description:''}"/>
    <aura:registerEvent name="InsertAccount" type="c:DMLEvent" description="Fire an DML event on saving the record"/>
    <lightning:layout >
        <lightning:layoutItem >
            <lightning:input type="text" label="Name" value="{!v.acc.Name}" name="nameOfAcc"/>
            <lightning:input type="text" label="Description" value="{!v.acc.Description}" name="des"/>
            <lightning:button type="button" label="Save Account" onclick="{!c.saveAccount}" />
        </lightning:layoutItem>
    </lightning:layout>
</aura:component>

Component Controller:

({
    saveAccount : function(component, event, helper) {
        console.log('########');
        var initializeDMLInfo =$A.get('e.c:DMLEvent');
        initializeDMLInfo.setParams({'sObjectInfo':component.get('v.acc')});
        initializeDMLInfo.fire();
    }
})

Event:

<aura:event type="APPLICATION" description="Event template" >
    <aura:attribute name="sObjectInfo" type="Sobject" />
</aura:event>

Event Handler Component:

<aura:component >
    <aura:handler name="handleEvent" event="c:DMLEvent" action="{!c.doDML}" />
</aura:component>

Event Handler Components Controller:

({
    doDML : function(component, event, helper) {
        console.log('#########'+event.getSource());
    }
})
4

There are 1 or 2 events in your code but from what i understand you are firing an application event. Application event handlers cannot have the name attribute so remove the name attribute from event handler component

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

<aura:component >
    <aura:handler event="c:DMLEvent" action="{!c.doDML}" />
</aura:component>
  • Here is my updated component. I am still not able to fire the event.<aura:component > <aura:handler event="c:DMLEvent" action="{!c.doDML}" /> </aura:component> – Abhinay Saripalli Mar 17 '18 at 16:19
  • @AbhinaySaripalli, Are you not able to fire the event or is the handler not getting invoked? – RedDevil Mar 17 '18 at 16:43
0

Got the answer. Problem is my Event Handler component i didnt specify in my Lightning app. We have to specify all our components which are going to communicate in our application in the Lightning app, then only they can know each other and can able to fire.

Before changes :

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

After changes:

<aura:application extends="force:slds" >
    <c:AccountCapture />
    <c:DMLComponent />
</aura:application>

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.