Skip to content

Async Button

Purpose: Run one call to the server from a button that is switched off until the call settles, so a second click sends nothing and the button comes back if the call fails.


Why an Async Button?

A plain button that calls the server stays clickable while it waits. A double click sends the call twice, which for a save or a duplicate means two rows. The async button provides:

  • One call for any number of clicks while it is running
  • A disabled state for the length of the call, and a spinner in the template version
  • A reset when the call fails, so the user can try again
  • A flag that several buttons can share, so only one of them runs at a time
  • One attribute to add the behaviour to any element, with no template

Quick Start

1. Include the button

{% include 'django_spire/button/async_button.html' with x_button_click='component.publish()' button_text='Publish' button_icon='bi bi-send' %}

The button calls component.publish(). While that runs it is disabled and shows a spinner in place of its icon.


Core Concepts

async_button.html

The button as a template, at django_spire/button/async_button.html.

Argument Description
x_button_click The call to wait for, as an Alpine expression such as component.save()
x_busy Optional. The name of a flag in an enclosing Alpine scope that several buttons share
button_text The label
button_class Bootstrap button classes. Defaults to btn-primary text-light
button_icon Optional icon classes, such as bi bi-trash
button_title Optional tooltip

asyncButton

The behaviour itself, registered with Alpine by django_spire/js/components/async_button.js. The template is this plus markup.

asyncButton(call, busyName)
Argument Description
call A function that returns a promise. The button is off until it settles
busyName Optional. The name of a shared flag, as for x_busy
Name Description
isRunning True while this button's own call is running
isBusy True while this button is off: its call is running, or the shared flag is set

Main Operations

Adding the Behaviour to Any Element

<button
    class="btn btn-sm btn-outline-primary"
    type="button"
    title="Duplicate Task"
    x-bind="asyncButton(() => component.duplicate({pk: {{ task.pk }}}))"
>
    <i class="bi bi-copy"></i>
</button>

x-bind="asyncButton(...)" gives an element the click handling and the disabled state with one attribute. Use it for an icon button, or any button whose markup the template does not fit.

Running Two Steps

<button
    class="btn btn-sm btn-outline-success"
    type="button"
    x-bind="asyncButton(async () => {
        await item.complete()
        refreshItem(item.$pk)
    })"
>
    Complete
</button>

The function must return a promise that settles when the work is done. A single call does that already, so () => component.save() is enough. When something has to happen after the call, mark the function async and await the call, as here. Without that the button would switch back on before the work had finished.

Sharing a Flag Between Buttons

<div class="d-flex gap-2" x-data="{ isSaving: false }">
    {% include 'django_spire/button/async_button.html' with x_button_click='component.save_draft()' x_busy='isSaving' button_text='Save Draft' button_class='btn-outline-secondary' %}
    {% include 'django_spire/button/async_button.html' with x_button_click='component.publish()' x_busy='isSaving' button_text='Publish' %}
</div>

Buttons that name the same flag take turns. The one that was clicked sets the flag for the length of its call, and the others are off while it is set. Declare the flag on an element around the buttons.

Using It on an Element With Its Own Alpine Data

<button
    class="btn btn-primary text-light"
    type="button"
    x-data="asyncButton(() => component.publish())"
    x-bind="button"
>
    <span x-show="isRunning" class="spinner-border spinner-border-sm me-1" role="status"></span>
    Publish
</button>

The one-attribute form gives the element its Alpine data. An element that must set x-data itself uses asyncButton as that data and binds its button. The result is the same.


Things to Know

  • A failed call is not hidden. The button switches back on and the failure is passed on. For a Glue call, Spire's base.html shows the user an error message.
  • It is not Alpine's x-bind:disabled alone. Disabling a button in the page takes a moment to apply, so two clicks in the same instant can both get through. asyncButton also checks before it starts.
  • A project that does not extend Spire's base.html must load the scripts in django_spire/js/components/ itself.