Actions
The magic sauce. Actions allow you to easily listen to page interactions and call a method on your Wires.
Actions provide an effortless way to track page interactions and invoke methods on your Wires, resulting in a re-render of the Wire's Template.
Here is a basic example of how to use it:
Invoking Actions
You can listen for browser events and invoke actions using a selection of various Directives. The directives follow the format: wire:[browser event]=[action].
Some examples of events you can listen for include:
Event | Directive |
---|---|
click | wire:click |
keydown | wire:keydown |
submit | wire:submit |
Here are a few examples of each in HTML:
You can listen for any browser events on elements by using the wire:[event] directive, where [event] is the event's name. For example, to listen for a "foo" event on a button element, you would use the following code:
Passing Parameters
You can pass parameters to your actions, such as here using addTask('Some Task')
.
The parameter is then passed through to your Actions via function arguments.
Return Values
Actions shouldn't return any value. Return values are ignored.
Accessing Data Properties
You can access data properties inside your actions directly using data
.
Accessing Computed Properties
You can access Computed Properties inside your actions directly using computed
.
Notice that our Computed Property above is defined as a closure function, but once our deleteTasks() action is called, the computed property has already been rendered.
Magic Actions
In CBWIRE, there are some "magic" actions that are usually prefixed with a "$" symbol:
Function | Description |
---|---|
$refresh | Will re-render the component without firing any action |
$set( 'dataproperty', value ) | Shortcut to update the value of a property |
$toggle( 'dataproperty' ) | Shortcut to toggle boolean properties off and on |
Consider the example below.
You can instead call $set and avoid the need to create an Action named setMessageToHello.
It can also be used in the backend when listening for an event. For example, if you have one component that emits an event like this:
Then in another component you can use a magic action for example $refresh() instead of having to point the listener to a method:
Last updated