Template attributes
The trigger attributes are exactly the same as in the PHP version (the same linker.js), and they're also described at the top of resources/js/cg/linker.js. Every triggering element must be inside a Linker mount point.
Attributes
| Attribute | Effect |
|---|---|
data-linker="alias" etc. | The mount point, generated by linker::mount(...).html(); don't write it by hand |
data-linker-call="method" | Which method to call (must be in METHODS) |
data-linker-args='{"id":3}' | Arguments: a JSON literal |
data-linker-args="form" | Arguments: every field of the nearest <form> (not file bytes); name[] becomes an array |
data-linker-args="keyword,tab" | Arguments by name: looks for a field with that name first, then client state |
data-linker-value="user" | Merges this element's own value into the arguments under this key |
data-linker-extra='{"id":12}' | Fixed values, merged last; a field with the same name can't override them |
data-linker-on="input" | The triggering event. Defaults: click for buttons, change for form elements; comma-separate for several |
data-linker-debounce="400" | Debounce (milliseconds) |
data-linker-confirm="Sure?" | Confirm before sending |
data-linker-confirm-danger | Destructive: danger style + two clicks needed |
data-linker-confirm-title / -label | The callout title and confirm button text |
data-linker-confirm-target="#host" | Which container the callout is inserted into |
data-linker-refresh-on-reconnect | Refetch when the connection comes back (reads only) |
data-linker-bind='[…]' | Several triggers on one element; see below |
Attributes on the mount point (generated by linker::mount):
| Attribute | Stateful | Stateless |
|---|---|---|
data-linker | alias | alias |
data-linker-instance | uuid | — |
data-linker-rev | 0 | — |
data-linker-mode | — | stateless |
data-linker-url | — | The signed URL |
data-linker-min-delay | 800 | 800 |
Adding them in Rust and templates
// a component builder
Button::new("+1").attribute("data-linker-call", "increment");
Button::new("Delete")
.color(Color::Danger)
.attribute("data-linker-call", "delete")
.attribute("data-linker-args", json!({ "id": customer.id }).to_string())
.attribute("data-linker-confirm", "This can't be undone")
.attribute("data-linker-confirm-danger", true); // true → outputs data-linker-confirm-danger=""{# attrs on a template function #}
{{ cg_button("Save", type="submit", attrs={"data-linker-call": "save", "data-linker-args": "form"}) }}<!-- or written directly on an ordinary element -->
<input name="keyword" data-linker-call="search" data-linker-on="input" data-linker-debounce="400" data-linker-args="keyword">Build JSON arguments with serde_json rather than concatenating strings: the attribute value is escaped to {"id":3}, and the browser restores it when it reads the attribute.
Building arguments
<!-- 1. Fixed JSON -->
<button data-linker-call="show" data-linker-args='{"page":2}'>Page 2</button>
<!-- 2. A whole form (on the submit button) -->
<form><input name="name">
<button type="submit" data-linker-call="save" data-linker-args="form">Save</button>
</form>
<!-- 3. Named fields -->
<input name="keyword" data-linker-call="search" data-linker-on="input"
data-linker-debounce="400" data-linker-args="keyword">
<!-- 4. A whole form + an id the server already knows -->
<button data-linker-call="update" data-linker-args="form" data-linker-extra='{"id": 12}'>Update</button>
<!-- 5. A select sending its own value -->
<select data-linker-call="assign" data-linker-value="user" data-linker-extra='{"ticket": 7}'>…</select>Rust receives Args (serde_json::Map<String, Value>):
let id = args.get("id").and_then(Value::as_i64);
let keyword = args.get("keyword").and_then(Value::as_str).unwrap_or_default();Form fields always arrive as strings ("5", not 5); only JSON literals keep their types. To accept both, do what the example project's Counter::setStep does:
let step = args.get("step").and_then(|v| v.as_str()?.parse::<i64>().ok().or(v.as_i64()));To read everything into a struct at once, use serde_json::from_value::<MyArgs>(Value::Object(args)), and return LinkerResponse::validation when it fails.
extra isn't authorization
An id in data-linker-extra is still input the user can change. The server must check it with a policy; see Access control.
Collecting a whole form
Put data-linker-args="form" on the submit button, not on the <form> itself.
When collecting a form, the runtime first dispatches a cancelable submit event on it, giving guards such as upload components a chance to stop it. A Linker trigger bound to the form's submit would be blocked by that very event; that's what the comment in the example project's runtime_demo.rs is about.
Use type="submit" on the submit button: the click's default action is prevented (prevent defaults to true for click), and without JS the form submits normally, which gives you progressive enhancement.
Confirmation
<button data-linker-call="delete"
data-linker-args='{"id": 12}'
data-linker-confirm="This can't be undone"
data-linker-confirm-title="Delete customer"
data-linker-confirm-label="Delete"
data-linker-confirm-danger>Delete</button>Several triggers on one element: data-linker-bind
"Check while typing, submit when leaving the field" uses different methods at different times. PHP uses @linkerBind; Rust uses linker::bind, which checks the keys and returns the attribute name and value:
let (name, value) = linker::bind(&json!([
{ "on": "input", "call": "preview", "debounce": 300, "args": "keyword" },
{ "on": "blur", "call": "commit", "args": "form" },
]))
.map_err(laravel::Error::msg)?;
let input_attrs = { let mut a = Attributes::default(); a.set(&name, value); a };Each entry can have on / call / args / value / extra / debounce / prevent / confirm; when confirm is an object it accepts only message / title / label / target / danger. An unknown key returns Err.
| Rule | Behavior |
|---|---|
| The later one cancels the earlier | A new trigger clears debounces still pending on the same element |
| Timers are per binding | Kept on the element, so they don't cancel each other |
prevent defaults only for click and submit | Other events don't block the default action |
Don't send pure front-end preprocessing through Linker
Formatting, counting characters and previews are jobs for data-state-*. Keep the second binding for checks that really need the server (duplicates, amounts). Otherwise Linker becomes a keyup handler.
The mount point and the object are two different things
let attrs = mount.html() + &runtime::render_object("customerPanel", &[Allow::Render, Allow::StateRead, Allow::StateWrite]).render(&[]);linker::mountdecides "which component is called"render_objectdecides "whether this block of DOM may read and write state, and render"
For the data a component returns to be written to state and shown with data-state-text, the matching permissions are needed. You need both.
Two common mistakes
- Triggering elements go inside the mount point:
data-linker-callonly binds descendants of the mount point, not the mount point itself. - Don't put the mount point inside a block that render fragments redraw: every redraw throws away the old instance.