Can we pass JS event into func ? #678
-
can we pass js events into go functions ? I am using templ func to run functions in my htmx request and its working fine, but I want to access the event and pass them on my functions. so something like getting the event from hx-on::before-request into I am open for other workarounds if this is not possible |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
For frontend code to call Go functions you're going to have that function available via wasm and running in the browser separate from the server Go code with all the implications of it being two separate processes that you'll need to keep in mind, or you will need to make that function. Be something that calls the back end and links to that specific function on the backend, retrieves the answer and then uses it in the frontend. Both are possible, but javascript in the browser is running in a physically different location than your go code right now. So one of these two things would need to be implemented first, and there are enough ways someone might want this done that probably you wouldn't want in templ. Something that might be confusing is that it looks like the reverse is possible. You can put go values in JavaScript using the templating available. That's because you are rendering the JavaScript with those values, but once that is sent to the front end, the values you got from the back end are already there and you would need to communicate to the back end to get more or different data. Reloading the page with different query parameters, the backend could send different data because it would be another round trip to the server, but it isn't actually go communicating with JavaScript, it's go templating JavaScript with some values in the JavaScript already printed out to be interpreted by the browser when it arrives there. |
Beta Was this translation helpful? Give feedback.
Yes!!!! How could I be soo dumb, they are literally running on different environments. Ngl templ makes it feel like writing reactive code in the client. My bad.