JavaScript

CBWIRE includes Alpine.js, and uses Livewire.js for all client-side functionality and DOM diffing. These tools give you complete control over client-side functionality while providing ways to interact with your component server-side.

Alpine.js

Alpine.js is included with CBWIRE and was designed to work beautifully with your CBWIRE components.

Using Alpine.js is as easy as declaring an x-data in your template. You can declare client-side properties, actions, and interact with your component using the $wire object.

<div
    x-data="{
        counter: 0,
        increment() {
            this.counter++   
        },
        async save() {
            // Call the save method on our component
            await $wire.save( this.counter );
        }
    }"
    wire:ignore.self>
    <div>Count: <span x-text="counter"></span></div>
    <button @click="increment">+</button>
    <button @click="save">Save to database</button>
</div>

Learn more on the Alpine.js features page.

$wire object

With $wire, you get a JavaScript representation of your server-side CBWIRE component that you can interact with. You can change data properties, call actions, access parent components, fire events, and much more.

Below are the properties and methods of the $wire object:

Executing Scripts

You can execute bespoke JavaScript in your CBWIRE component using <cbwire:script>.

Here's a complete example:

Scripts are executed after the page has loaded BUT before your CBWIRE component has rendered.

Loading Assets

You can load style assets on the page with your component using <cbwire:assets>.

Here's an example of loading a date picker called Pikaday:

Livewire will ensure any assets are loaded on the page before evaluating scripts. It will also ensure that assets are only loaded once per page, even if you have multiple instances of the component.

Using $wire From Scripts

You can access your component's $wire object within your scripts block.

Livewire Events

You can access the livewire:init and livewire:initialized events as Livewire is loading.

Livewire object

You can interact with Livewire from external scripts using the Livewire global object.

Custom Directives

Livewire allows you to register custom directives using Livewire.directive().

Here's the implementation for wire:confirm:

JavaScript Hooks

Intercepting Commits

A commit is made every time a CBWIRE component is sent to the server. You can hook into an individual commit like this:

Request Hooks

Using the request hook, you can hook into the entire HTTP request, which may contain multiple commits.

Customizing Page Expiration

If the default page expiration dialog isn't suitable for your application, you can use the request hook to implement a custom solution.

Was this helpful?