-
-
Notifications
You must be signed in to change notification settings - Fork 280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Introduce JsGenericVar for Event and Element Access in element event handlers #850
Comments
Same, I also had the same issue. It would be great to access the I am not familiar with the implementation of how script function in templ is transpiled into JS, but it seems like one generic type like Once cavity with this is, templ would not be able to sanitize this |
@iambpn, what do you mean by sanitization? |
Initial implementation in PR #851 |
By sanitization, I mean escaping unwanted characters like This is also mentioned here in the docs. link |
Aah okay. Yeah this applies to values exported from GO to JavaScript. But since the event variable only exists in the JavaScript runtime this does not have much effect I think. |
Fixed in #851 - thanks contributing! |
While using temple i came acros the following issue:
When defining a
script
that will be added to theonclick
attribute of an element, there is currently no way to access the event or the target element in the handler. This would have me end up doing additional querySelectors to access the target element and, maybe most important, all event metadata is completely lost in this way.Solution
Introduce a JsGenericVar string type. This type can be used to set the signature of the function and be replaced by the string value as a JS variable during generation.
This would enable access to any global JS variable in a Templ script block.
Since there is no type-checking in the script blocks (because it's JavaScript), this string type should work seamlessly.
Generated HTML/JS Code
The generated HTML/JS code would look something like this:
Predefined Constants
Define some constants for the most common use cases: events and target element reference.
This adds more explicit declarations, enhancing readability since the inferred JS variable is also stated in the script signature.
Feedback and insights on this would be appreciated!
The text was updated successfully, but these errors were encountered: