[cbWire 4.x] dispatchTo() doesn't seem to be working

I’m working on a small application using cbWire 4.1 and ColdBox 7.3. Trying to use dispatchTo() to emit an event to another component. It doesn’t seem to be working or perhaps, I didn’t use the method correctly. Could someone lighten the shade? Thanks. @gcopley

addPost() was logged correctly. However, TopBar component didn’t get called.
Below is the code (improvised version from the cbWire docs) I use to try the method.

<cfoutput>
#wire("Posts")#
</cfoutput>
// ./wires/Posts.cfc
component extends="cbwire.models.Component" {

	property name="log" inject="LogBox:logger:{this}";

	function addPost() {
		log.info("Add a new post and increment counter");
		js('
			console.log("Add a new post and increment counter");
		');

        dispatchTo( "TopBar", "post-added", {} );
    }

    function renderIt() {
		return template("wires.posts");
	}

}
<!--- ./wires/posts.cfm --->
<cfoutput>
<div>
	<button class="btn btn-primary" wire:click="addPost">Add Post</button>
</div>
</cfoutput>
// ./wires/TopBar.cfc
component extends="cbwire.models.Component" {

	property name="log" inject="LogBox:logger:{this}";

	listeners = {
		"post-added": "incrementPostCount"
	};

	function incrementPostCount() {
		log.info("Increment the post count in TopBar");
		js('
			console.log("Increment the post count in TopBar");
		');
	}

}
<!--- ./wires/topBar.cfm --->
<cfoutput>
<div>
	<!--- Empty --->
</div>
</cfoutput>

I would start with opening your Javascript console and run:

Livewire.all().forEach( obj => console.log( obj.name ) );

It will output the name of all wires on the current page. Then you can verify you have one named “TopBar”

1 Like

@Ancient_Programmer I took the code snippets you posted ( thanks for providing those BTW ) and tried this out locally and it appears to be working for me. I added a timestamp output to verify that TopBar is updating, and you can see in the console log that incrementPostCount() is being called.

I would try what @MikeR suggested and make sure you see TopBar listed.

Also for good measure, do a fresh install of cbwire 4.

box install cbwire@4 --force

Let us know how it goes.

2024-10-30 11.17.22

Both answers from @MikeR and @gcopley has solved the problem. Thanks so much!

2 Likes