5 ms·
Hmmmm... no. This: <button hx-post="/clicked" hx-swap="outerHTML"> Click Me </button> Should be just this: <button onclick="htmx.post('/
by TekMol 3y ago
Hmmmm... no.
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.
- the_gipsy 3y agoYou're missing the "swap".
- TekMol 3y agoIt's always an interesting question how explicit one wants to get. In this case, I would say the 'outerHTML' parameter is enough to define what will be done with the post result.
- yscodes 3y agoI like your change. Much clearer. Makes me more interested than the current syntax.
- listenallyall 3y agoYour opinion is totally valid. However, is it really difficult or tricky to recognize that 'click' the default event of a 'button' tag? Isn't 'onclick=' kind of redundant?
- recursivedoubts 3y agowe don't specify "click" on anchors or "submit" on forms, do we?
- mekoka 3y ago> no magic would be needed, a lot of indirection and complexity would be avoided 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.
- renerick 3y agoYou are missing the fact that html has very restricted list of events that can be defined in an attribute. Htmx allows to listen on any event, plus some extra features on top, such as event throttling, polling, synchronization between elements. Stylistically I can see the appeal of your proposal and even agree on some ways, but it's not functionally equivalent https://htmx.org/attributes/hx-trigger/ https://htmx.org/attributes/hx-trigger/ https://htmx.org/attributes/hx-sync/ https://htmx.org/attributes/hx-sync/
- TekMol 3y agoWhat would be a simple example of an event that you want to define inline but can't in plain html? Can you give minimal example on jsfiddle so we can compare different approaches?
- renerick 3y agoHere you go: https://plnkr.co/edit/DQJrBWWQFgHtpltf?open=index.html&preview https://plnkr.co/edit/DQJrBWWQFgHtpltf?open=index.html&previ... 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/ https://htmx.org/examples/sortable/ [2]: https://htmx.org/headers/hx-trigger/ https://htmx.org/headers/hx-trigger/ [3]: https://htmx.org/attributes/hx-trigger/#triggering-via-the-hx-trigger-header https://htmx.org/attributes/hx-trigger/#triggering-via-the-h... [4]: https://htmx.org/examples/update-other-content/ https://htmx.org/examples/update-other-content/
- TekMol 3y agoHmm... that does no look like a minimal example to me. Is this the relevant line? <div hx-get="/random" hx-trigger="every 2s"></div> If so, can you make an example with just that?
- the_gipsy 3y agoThink of the tags as a constraint. It's quite easy to check that certain constraints are met with tags. It's much harder to check on this <onevent>="htmx.<action>(<url>, <swap>)" convention. Soon you'll have unconstrained code creeping in (e.g. onclick="!loading ? htmx.post('/clicked', 'outerHTML') : void(0)").
- recursivedoubts 3y agoDo you consider the href attribute of anchors "magic"?