Created
June 15, 2024 20:01
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
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 --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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