Triggers and listeners
"What the user did → which method is called → who can hear about it afterwards". Everything on this page is browser behavior, identical to the PHP version; only the way attributes are produced differs.
Triggering events
| Element | Default event |
|---|---|
| Buttons, links, etc. | click |
input / select / textarea | change |
Change it with data-linker-on:
<input name="keyword" data-linker-call="search" data-linker-on="input" data-linker-debounce="400" data-linker-args="keyword">
<!-- the same method at several moments -->
<input name="qty" data-linker-call="quote" data-linker-on="change,blur" data-linker-args="qty">
<!-- form submission -->
<form data-linker-call="save" data-linker-on="submit" data-linker-args="form">…</form>prevent (blocking the default action) defaults only for click and submit. Bound to submit, it blocks the browser's native submission, so nothing is sent twice.
One element, different moments, different methods: data-linker-bind
PHP's @linkerBind is linker::bind in Rust, returning the attribute name and value:
use cg_component::runtime::linker;
use serde_json::json;
let (name, value) = linker::bind(&json!([
{ "on": "input", "call": "checkEmail", "debounce": 300, "args": "email" },
{ "on": "blur", "call": "saveDraft", "args": "form" },
]))
.map_err(laravel::Error::msg)?;
let mut input_attrs = Attributes::default();
input_attrs.set(&name, value); // data-linker-bind='[{"on":"input",…}]'You can also write it straight into a template (put the JSON in a single-quoted attribute):
<input name="email" data-linker-bind='[{"on":"input","call":"checkEmail","debounce":300,"args":"email"},{"on":"blur","call":"saveDraft","args":"form"}]'>| Rule | Behavior |
|---|---|
| The later one cancels the earlier | A new trigger clears debounces still pending on the same element (when blur arrives, an unsent input is dropped) |
| Timers are per binding | Kept on the element, so two buttons calling the same method don't cancel each other |
| Allowed keys | on call args value extra debounce prevent confirm; inside confirm: message title label target danger |
| A misspelled key | linker::bind returns Err |
The PHP version also checks event names, method names, the debounce type and duplicate "same event, same method" entries; Rust checks only the keys, and the browser ignores or warns about the rest.
Throttling and double clicks
| Mechanism | Scope | Behavior |
|---|---|---|
data-linker-debounce="400" | Element | Sends only after 400ms without input |
Minimum interaction interval (data-linker-min-delay, fixed at 800ms in Rust) | Element | Buttons start disabled after mounting and become clickable after the interval; after sending they stay locked until "the work is done and the interval has passed" |
| Busy during the request | Element | Disabled during the request, restored afterwards |
While locked: button triggers are dropped; input fields keep only the last one and send it when unlocked. Otherwise the server would never learn what the user finally chose.
linker::mount writes data-linker-min-delay on the mount point, with the value MIN_DELAY_MS.
Confirmation before sending
Button::new("Archive")
.attribute("data-linker-call", "archive")
.attribute("data-linker-args", r#"{"id": 7}"#)
.attribute("data-linker-confirm", "Archived rows no longer appear in the list")
.attribute("data-linker-confirm-title", "Archive")
.attribute("data-linker-confirm-label", "Archive")Add data-linker-confirm-danger for the danger style + two clicks. The server can also ask inside the pipeline (Action::confirm); see Timing.
Refetching on reconnect
<button data-linker-call="show" data-linker-args='{"page":1}' data-linker-refresh-on-reconnect>Refresh</button>For reads only: writes are never resent automatically (the request lost to a disconnect may already have been written). Arguments must be a JSON object; elements with data-linker-confirm don't run even when marked.
Form guards
Before sending, data-linker-args="form" first dispatches a cancelable submit event on the form. Guards such as upload components cancel it while they're unfinished, and Linker then silently doesn't send. So don't put onsubmit="return false" on the form.
Listening: who hears about it afterwards
The runtime attaches no API to window; it only dispatches DOM events:
| Event | When | detail |
|---|---|---|
cg:state | Every client state write (including data returned by Linker) | { key, value, previous } |
cg:rendered | After a fragment is inserted or dom.repeat inserts rows | { root, … } |
cg:error | Exceptions from rendering or subscribers | Exception information |
// resources/js/site.js or a file it imports
document.addEventListener('cg:state', (e) => {
if (e.detail.key === 'orderPanel:total') {
console.log('total', e.detail.previous, '→', e.detail.value);
}
});
document.addEventListener('cg:rendered', (e) => {
// when a third-party library needs to initialize on the new nodes
e.detail.root.querySelectorAll('[data-tooltip]').forEach(initTooltip);
});Declarative listening is data-state-*: when a value changes, bindings redraw themselves, with no listener to write.
Calling from JS
To call a Linker method from your own JS, use callLinker, exported by runtime.js:
import { callLinker } from './cg/runtime.js';
callLinker(element, 'search', { keyword: 'acme' }); // element must be inside a mount pointIt goes through the same queue, error handling and pipeline.
Chained triggers: one call, several things
The server returns several actions at once, run in order:
Ok(LinkerResponse::ok()
.data("saved", true)
.then(Action::toast("success", "Saved"))
.then(Action::state_clear("orderForm"))
.then(Action::new("render.fragment", json!({ "target": "#orders", "name": "orders.list", "fresh": true }))
.map_err(laravel::Error::msg)?)
.then(Action::scroll_to("#orders")))No Promise chaining in the browser; the execution rules are in Timing.