3

According to the component bundle documentation there is no .evt file in a component bundle.

So where should a component's event be defined? Eg. from here

<!--docsample:compEvent-->
<aura:event type="COMPONENT">
  <!-- add aura:attribute tags to define event shape.
    One sample attribute here -->
  <aura:attribute name="message" type="String"/>

</aura:event>

Where would this go? I tried putting it in my .cmp but it says "invalid attribute 'type'".

I can put it in a file called events.evt, but, if my component is dependent on that, and the .evt is not distributed with a component bundle (per the docs), it's not going to work.

Am I approaching this wrongly?

1 Answer 1

4

Lightning events should be put into their own directory, separate from the components - even for component events.

Here's an example that I was able to deploy and run:

./package.xml

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>*</members>
        <name>AuraDefinitionBundle</name>
    </types>
    <version>35.0</version>
</Package>

./aura/AppName/AppName.app

<aura:application >
    <*NameSpace*:ComponentName />
</aura:application>

./aura/AppName/AppName.app-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<AuraDefinitionBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>35.0</apiVersion>
    <description>A Lightning App</description>
</AuraDefinitionBundle>

./aura/ComponentName/ComponentName.cmp

<aura:component >
    <aura:registerEvent name="ComponentEventFired" type="*NameSpace*:EventName" />
    <aura:handler name="ComponentEventFired" action="{!c.handleComponentEvent}" />
    <ui:button press="{!c.fireComponentEvent}" label="Press Me" />
</aura:component>

./aura/ComponentName/ComponentName.cmp-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<AuraDefinitionBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>35.0</apiVersion>
    <description>Lightning Component</description>
</AuraDefinitionBundle>

./aura/ComponentName/ComponentNameController.js

({
  fireComponentEvent : function(cmp, event){
    console.log('The component event was fired.');
    var evt = cmp.getEvent("ComponentEventFired");
    evt.fire();
  },
  handleComponentEvent : function(cmp, event){
    console.log('The component event was handled.');
  }
})

./aura/EventName/EventName.evt

<aura:event type="COMPONENT" description="EventName" >
</aura:event>

./aura/EventName/EventName.evt-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<AuraDefinitionBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>35.0</apiVersion>
    <description>An event</description>
</AuraDefinitionBundle>

You must log in to answer this question.

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