Reference · Browser

ask

signature
ask(str, map | null=) -> bool

What it does

Asks a question in a dialog on the page and waits for the answer: true when OK is pressed, false for Cancel, Escape or a click on the backdrop. The options are title, ok and cancel (the buttons' words, "OK" and "Cancel" otherwise) and tone ("danger" starts with the focus on Cancel). It is called from an event handler or @mount of a component, which waits for the answer and goes on. On the server there is no one to ask, so there it is a runtime error.

Examples

in a program
server fn remove_note(id: int) = db.exec("delete from notes where id = ?", id)component Note(id: int, text: str) {    state gone = false    <li>        {gone ? "(removed)" : text}        <button @click={() => {            if ask(`Remove "${text}"?`, { title: "Remove", ok: "Remove", tone: "danger" }) {                remove_note(id)                gone = true            }        }}>Remove</button>    </li>}

Edge cases

Errors

On the server, in a route, a plain program or a server fn, ask is a runtime error, not an error value: it stops the request with a message that says where to call it.

this one fails
print(ask("Delete everything?"))
output
line 1: `ask` and `notify` speak to someone in the browser: call them from a component (an event handler)

Chaining

ask is not a pipeline function; it reads as a condition in a handler:

in a program
@click={() => { if ask("Sure?") { remove(id) } }}@click={() => { ask("Sign out?", { ok: "Sign out" }) && sign_out() }}

See also

notify, navigate, js

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On ask(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo