Data, state and bindings
How a ui reads values, repeats over lists, shows parts conditionally, and remembers what was typed.
A card has two bags of values. The ui reads from both.
data | state | |
|---|---|---|
| Set by | You, when sending or patching the card | The card itself, on the device |
| Lives | On Hookboard's server | In the browser tab showing the card |
| Typical content | What your system knows: totals, names, rows | What the person is doing: typed text, open tab, toggles |
| Changed by | POST /cards, PATCH /cards/{id}/data, or a patchData effect | Inputs with bind, the set, toggle, increment, append and remove actions, or a setState effect |
| Sent back to you | No (you already have it) | Yes, as state in every webhook call |
state isn't shared between devices, and it resets to the card's state field when the page reloads.
Reading a value
There are four ways to put a value into a property.
| Form | Example | Result |
|---|---|---|
| Template inside text | "Hello {{data.name}}" | Text with the value inserted |
| Template that is the whole text | "{{data.count}}" | The value itself, keeping its type (a number stays a number) |
| Binding object | { "$bind": "data.rows" } | The value at that path, keeping its type |
| Expression object | { "$expr": "data.total * 1.24" } | The result of the expression |
Inside {{ }} you can write any expression, not just a path: "{{formatNumber(data.total, 2)}} EUR".
{
"data": {
"name": "Ada",
"orders": 46,
"rows": [{ "day": "Mon", "n": 3 }, { "day": "Tue", "n": 5 }]
},
"ui": {
"type": "stack",
"children": [
{ "type": "heading", "text": "Hello {{data.name}}" },
{ "type": "stat", "label": "Orders", "value": "{{data.orders}}" },
{
"type": "barChart",
"data": { "$bind": "data.rows" },
"x": "day",
"series": [{ "key": "n", "label": "Orders" }]
}
]
}
}A path that doesn't exist gives you nothing: the text is empty, and there's no error.
Files sent with the card are read the same way, by name: {{files.chart}} is the address of the file sent as chart. See Files.
Repeating over a list
repeat draws an element once for each item of a list. Inside it, item is the current item and index its position, starting at 0. as renames item.
{
"data": {
"tasks": [
{ "title": "Write docs", "done": true },
{ "title": "Ship", "done": false }
]
},
"ui": {
"type": "stack",
"children": [
{
"type": "text",
"repeat": { "each": "data.tasks", "as": "task" },
"value": "{{index + 1}}. {{task.title}}{{task.done ? \" (done)\" : \"\"}}"
}
]
}
}each is an expression written without braces.
Showing something conditionally
visibleWhen is an expression written without braces. The element only shows while it's true.
{
"data": { "errors": 3 },
"ui": {
"type": "stack",
"children": [
{
"type": "alert",
"tone": "danger",
"title": "{{data.errors}} errors",
"visibleWhen": "data.errors > 0"
},
{
"type": "text",
"value": "All clear.",
"visibleWhen": "data.errors == 0"
}
]
}
}Inputs and local state
An input reads and writes one place in state, named by its bind property. The path must start with state..
{
"state": { "note": "" },
"ui": {
"type": "stack",
"children": [
{ "type": "input", "bind": "state.note", "label": "Note" },
{
"type": "text",
"value": "You typed: {{state.note}}",
"visibleWhen": "state.note"
},
{
"type": "button",
"label": "Save",
"onPress": {
"type": "webhook",
"url": "https://your-app.example/notes",
"payload": { "text": "{{state.note}}" }
}
}
]
}
}The button's payload is resolved at the moment it's pressed, so your URL gets whatever was typed. Every webhook call also includes the whole state object.
Buttons can change state directly with the device actions set, toggle, increment, append and remove. See the Actions reference.