Component syntax with expanded <script>
tags
#4100
markhuot
started this conversation in
3. Feature Ideas
Replies: 1 comment 4 replies
-
You coul make a simple magic that would do this. But you can write the component in a script tag...or functions in the script tag... and then access them. <script>
async function doStuff(form) {
const response = await fetch('/contact', { method: 'post', body: new FormData(form) })
if (response.ok) cleanData()
}
</script>
<form @submit="doStuff($el)">
<input type="text">
</form> I'm not sure that the approach you present there makes enough sense as an alternative to the existing system to justify it in the core. But a plugin to add a magic that would do it would be quite simple, but I do fear the DX.... |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I really like Alpine. Like—like, like, it. But one thing I struggle with is shoving a bunch of javascript in to HTML attributes. Even with PHPStorm able to inject the JavaScript language in to the attribute I still find over half of my Alpine bugs feel like catchable bugs where I miss a
;
on a multi-expression statement that I accidentally shoved in to a short closure, or something like that.For example, I write a lot of Alpine that looks like this,
I love the DX of Alpine—keeping my functionality and HTML close together feels right. But I keep finding myself wishing Alpine was more like JSX/React where we could write real JavaScript (or something that looks and feels like real JavaScript). Maybe something like this,
Here the
alpine/attribute
<script>
tag would be applied to whatever element immediately follows the tag. The benefit here is it's an actual<script>
tag so existing IDEs should highlight/type check/etc… it. All out of the box.Is this a crazy idea? is there a better way that I should know about?
Beta Was this translation helpful? Give feedback.
All reactions