Skip to content

Instantly share code, notes, and snippets.

@sombriks
Created June 15, 2024 20:01
Show Gist options
  • Save sombriks/84ea02723623188c72b56060fdceeeb2 to your computer and use it in GitHub Desktop.
Save sombriks/84ea02723623188c72b56060fdceeeb2 to your computer and use it in GitHub Desktop.
hx-dataset-include - keep your application state in the loop using this hacky, fast and simple extension.
<!--
Since the markup IS the application state, using data-* attributes to keep track of some specifics would be handy.
Luckily, HTMX is brutally easy to extend, so we can do that in no time!
-->
<article class="message task"
th:id="'task'+${task.id}"
th:data-task="${task.id}"
th:data-status="${task.status.id}"
th:hx-put="@{/task/{id}(id=${task.id})}"
hx-ext="hx-dataset-include"
hx-trigger="put-task"
draggable="true"
@dragstart="e => e.dataTransfer.setData('text/plain', $el.id)"
@update-status.window="e => {
if(e.detail.taskEl == $el) { // needed to filter real target
$el.dataset.status=e.detail.lane.dataset.status
$dispatch('put-task', $el.dataset)
}
}">
<!-- rest of the markup -->
htmx.defineExtension('hx-dataset-include', {
encodeParameters: function (xhr, parameters, elt) {
Object
.keys(elt.dataset)
.forEach(k => parameters
.append(k, elt.dataset[k]))
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment