Events
You can emit events from your cbwire components, views, and also direct from JavaScript. Listeners can be defined to execute specific actions.
You can fire events from within views, components, or by using the global cbwire JavaScript object.

From View

1
<button wire:click="$emit( 'counterIncremented' )">
Copied!

From Component

1
this.emit( "counterIncremented" );
Copied!

From Javascript Global

1
<script>
2
cbwire.emit( 'counterIncremented' );
3
</script>
Copied!

Event Listeners

You can register event listeners on a component by defining variables.listeners.
1
component extends="cbwire.models.Component"{
2
3
variables.listeners = {
4
"counterIncremented": "tweetAboutIt"
5
};
6
7
function tweetAboutIt(){
8
// Blow up Twitter
9
}
10
11
...
12
}
Copied!
cbwire will invoke the tweetAboutIt method on the component if any other component on the same page emits a counterIncremented event.
When defining your listeners, it is good to put your listener's names in quotation marks, as we have in our component above. "counterIncremented": "tweetAboutIt"
JavaScript keys are case-sensitive. You can preserve the key casing in CFML by surrounding your listener names in quotations. Without the quotations, CFML will convert the key to all uppercase, such as TWEETABOUTIT.
🙃

Event Listeners In JavaScript

1
// File: ./views/wires/myView.cfm
2
<script>
3
cbwire.on( 'counterIncremented', counter => {
4
alert( 'The counter was incremented with a value of: ' + counter );
5
})
6
</script>
Copied!
Last modified 2mo ago