Comment by ranger_danger

Comment by ranger_danger a day ago

4 replies

I know I will get downvoted for this and people will just say "you're doing it wrong" or "the library wasn't designed for that" but I still think it would be really helpful for some people if the source of the data to swap out could be retrieved from parsing the response data as JSON and grabbing a certain named key.

Yes one can add a callback function to parse the data as JSON, do error handling etc. and then swap the text/HTML, but having that built in to a hx-foo attribute would be a lot easier.

evolve-maz a day ago

Htmx has events you can listen to like htmx after response. You can think of it almost like a middleware. After the response comes in, your callback is triggered and you can make the callback look up some attribute given the calling parents attribute that you might call hx-json-key.

Yes you have to add this yourself, but you only need to add one js function once and be done with it.

I've used the callback pattern for custom error handling for all hx responses.

wild_egg a day ago

I'm really struggling to think of a scenario where I would want to do something like that.

What use case do you have in mind?

  • ranger_danger a day ago

    Re-using existing API calls that I can't change, where I want to replace the text of an html element with the text from a specific key in a json response.

test1072 a day ago

No idea much about htmx but can we write custom extension something to add to htmx ?