3 ms·
Sounds like you are going through similar struggles as a few backend developers I had trying out htmx. Years of muscle memory with returning JSON and letting th
by masfoobar 2mo ago
Sounds like you are going through similar struggles as a few backend developers I had trying out htmx. Years of muscle memory with returning JSON and letting the frontend (javascript/jquery) to handle the html.
They were making a big deal with this change. In the end the change is rather minimal. The outcome is reduced javascript code and more server side html templates. Pretty much most of the code is now handled on the server side. Just organise the html templates!
Pseudo example
Instead of :-
<code>
// returning as Json
[Get]
Response GetUser(int id) {
var user = getUser(id);
return ToJson(user);
}
</code>
You are just doing :-
<code>
// returning as HTML
[Get]
Response GetUser(int id) {
var user = getUser(id);
return View("som-user-template", user);
}
</code>
I don't know what 'hooks' you need, but whatever they are doing you can move them over to returning/updating section of html on the screen with htmx.
- cpburns2009 2mo agoBy wiring up hooks I just mean binding a callback to a button that calls fetch() and processing the response (usually replacing HTML, but may modify content and classes on other elements). Basically what I'm looking for is being able to specify a callback (ideally just a global name set from HTML) that will be called with a response object when JSON is returned. I prefer most coding to happen server side, but sometimes client side JS is needed.
- masfoobar 2mo agoBut you can do this with htmx by using the respected hx-* attributes. For example, on button click to GET html. Again, you can specify what you want to do with the returned html with attributes such as hx-swap Unless I am misunderstanding, of course. I do remember triggers/hooks with things like Angular and Knockout. I don't miss this method of web development. I recommend starting small and working up with htmx. Their examples section on the official website is pretty good.