Events

A simple way for UI components to communicate with each other.

Events and listeners provide an elegant means for your components to communicate with each other. You can emit events from one component, listen for them on another component, and then execute code and re-render the listener.

Emitting Events

You can fire events from:

  • Actions using emit(), emitUp(), emitSelf(), and emitTo()

  • Templates using $emit()

  • JavaScript using cbwire.emit()

<!--- Action --->
<cfscript>
    function addPost() {
        emit( "postAdded" );
    }
</cfscript>
<!--- Templates --->
<cfoutput>
    <div>
        <button wire:click="$emit( 'postAdded' )">Add Post</button>
    </div>
</cfoutput>

Passing Parameters

You can send parameters as your emitting an event.

Scoping Events

emitUp

When working with nested components, sometimes you may want to emit events only to parent components and no children or sibling components.

For this, you can use the emitUp() method.

emitTo

You can emit events to components of a specific type ( name ) using emitTo().

emitSelf

You can emit events to the component that fired the event using emitSelf().

Listeners

You register event listeners using the listeners struct of your component.

Listeners are a key/value pair where the key is the event to listen for, and the value is the method to invoke on the component.

JavaScript keys are case-sensitive. You can preserve the key casing in CFML by surrounding your listener names in quotations.

Dispatching Browser Events

You can dispatch browser events using the dispatch() method.

You can listen for the event using vanilla JavaScript:

If you are using AlpineJS, you can listen for the event like this.

Last updated