Emit event outside cbwire

It’s possible to emit an event outside of a cbwire component? i.e. from a normal handlers/template.

Thanks

Yes, it is. The key is to grab a reference to the wire when it first loads.

There is a good example at cbwire.ortusbooks.com

This is the trimmed down simplified way I usually do it:

    <script>
        if( !window.hasOwnProperty('someSpecificWireOnPage') ){
            window.someSpecificWireOnPage = "";
        }
        document.addEventListener("livewire:load", function() {
            window.someSpecificWireOnPage = cbwire.find('#args._id#'); // args._id contains the id of our wire
             
        } );
    </script>

After the initial page load, you have a reference to that specific wire object on the page.

You can emit events like this:

window.someSpecificWireOnPage.emit('yourWireEvent');

and/or emit events with arguments like this:

window.someSpecificWireOnPage.emit('yourWireEvent', 'foo', 'bar');

I hope this helps.