fresh-controller-36545
04/10/2021, 10:22 PMgorgeous-ghost-95789
04/10/2021, 10:24 PMfresh-controller-36545
04/10/2021, 10:30 PMgorgeous-ghost-95789
04/10/2021, 10:31 PMgorgeous-ghost-95789
04/10/2021, 10:32 PMic-on-success to understand what it did/does, but I'd imagine there's a limit to what you can put in the attributes of a single tag.gorgeous-ghost-95789
04/10/2021, 10:34 PMfresh-controller-36545
04/10/2021, 10:37 PMgorgeous-ghost-95789
04/10/2021, 10:38 PMic-on-success. I think there's an event htmx:beforeSwap that gets pretty close. You could hook into that, and evaluate something in the xhr and then cancel the event if you don't like it.gorgeous-ghost-95789
04/10/2021, 10:38 PMfresh-controller-36545
04/10/2021, 10:38 PMfresh-controller-36545
04/10/2021, 10:39 PMgorgeous-ghost-95789
04/10/2021, 10:39 PM<div hx-get="/my-server" _="on htmx:beforeSwap(xhr) if xhr.headers.nope then halt">gorgeous-ghost-95789
04/10/2021, 10:42 PMgorgeous-ghost-95789
04/10/2021, 10:44 PM<script type="text/hyperscript">
behavior onSuccess(args)
on htmx:beforeSwap
if event.headers.nope
halt -- this cancels the event, and therefore the swap
end
end
end
</script>
<div hx-get="/my-server" _="install onSuccess"></div>gorgeous-ghost-95789
04/10/2021, 10:45 PMfresh-controller-36545
04/10/2021, 10:46 PMgorgeous-ghost-95789
04/10/2021, 10:46 PMgorgeous-ghost-95789
04/10/2021, 10:46 PMgorgeous-ghost-95789
04/10/2021, 10:48 PMgorgeous-ghost-95789
04/10/2021, 10:59 PM<script type="text/hyperscript">
on htmx:beforeSwap from .magicalCSS -- match event based on CSS class
fetch /validation-url with as json -- async process (go to another server, or something)
if it.isNotOK then -- halt the swap if you want to
halt
end
</script>
<div hx-get="/my-server" class="magicalCSS"></div>
Hyperscript is really great at making event handlers readable, which is a lot better than this Javascript:
javascript
window.addEventHandler("htmx:beforeSwap", function(event) {
if (event.target.cssClass == "magicalCSS") { // too lazy to figure out contains right now...
fetch("/validation-url", args)
.then(function(response) { // Promises are awful to work with
var body = JSON.parse(response.body) // I think this is how to parse the JSON
if (body.isNotOK) {
event.cancel(); // OMG, done!
}
})
}
}
I'm typing fast, so I guarantee there are tons of problems with my JS. But hopefully it makes the point. You could absolutely use Javascript to look for htmx:beforeSwap events, and that probably makes sense for many people. But personally, I think hyperscript makes the process a lot happier. πfresh-controller-36545
04/10/2021, 11:04 PMmysterious-toddler-20573
04/11/2021, 12:50 AMmysterious-toddler-20573
04/11/2021, 12:51 AMmysterious-toddler-20573
04/11/2021, 12:56 AMmysterious-toddler-20573
04/11/2021, 1:02 AMonClick handlers in the DOM, but you can't embed onhtmx:success or whatever. This means you can have the locality of behavior (https://htmx.org/essays/locality-of-behaviour/) of the old intercooler.js attributes, but without blowing out the htmx attributes.
- hyperscript is more expressive than javascript, so you can handle more things directly inline in the DOM without a bunch of junky code: on click transition my opacity to 0 then remove memysterious-toddler-20573
04/11/2021, 1:03 AMmysterious-toddler-20573
04/11/2021, 1:03 AMsalmon-whale-14625
04/11/2021, 5:53 AMadamant-exabyte-92636
04/11/2021, 7:13 AMadamant-exabyte-92636
04/11/2021, 7:13 AM