by TekMol · 3y ago · 0 comments
This:
<button hx-post="/clicked" hx-swap="outerHTML">
Click Me
</button>
Should be just this: <button onclick="htmx.post('/clicked', 'outerHTML')">
Click Me
</button>
This way, no magic would be needed, a lot of indirection and complexity would be avoided and everybody who knows HTML and JS could immediately read and understand it: - When does something happen here?
When the button is clicked.
- What does happen?
htmx.post() is called.https://htmx.org/attributes/hx-trigger/ https://htmx.org/attributes/hx-sync/
Can you give minimal example on jsfiddle so we can compare different approaches?
Another big aspect of allowing to handle any event is integration with other JS libraries. See this basic example of interacting with Sortable JS [1]
Also, htmx has a response header called `HX-Trigger`[2] which lets server trigger custom events on the client and you can handle those events as well[3]. This is quite useful if you have interdependent data and you want to refresh regions on the page in response to user actions somewhere else (for which htmx allows multiple different patterns for different scenarios)[4]
[1]: https://htmx.org/examples/sortable/
[2]: https://htmx.org/headers/hx-trigger/
[3]: https://htmx.org/attributes/hx-trigger/#triggering-via-the-h...
Is this the relevant line?
<div hx-get="/random" hx-trigger="every 2s"></div>
If so, can you make an example with just that?Is this an informed criticism based on your experience having worked with htmx, or is it just an off the cuff opinion?
Personally, I don't think the goal should be "no magic", or "no indirection", or "no salt", or no "sugar". I feel that aiming for just the right amount of those things, to have the goody without crossing over to disgust, is a sound goal.
In this case, I would say the 'outerHTML' parameter is enough to define what will be done with the post result.
Makes me more interested than the current syntax.