-
Hello! So here is our headache: We are building an application in ASP.NET Core with Razor Pages. The reusable "Component":
Now that part works well. When a value is selected in that autocomplete, HTMX is used to get new html dependent on what was selected in the autocomplete (lets say I picked "Fruits" then now we get a list of all Fruits ) So now, htmx puts another autocomplete (which use the same component, but with the new data, and without the optional htmx parameters)
Though we get an error "Alpine express error: autocompletexxxxxx() is not defined". When I look in the dom though, the script is right there with the function available. The function is callable from the console I hope this is clear enough, if you need me to be more precise or to rephrase better, I will do. I just cannot show the actual code |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Try putting the script tag before the component. The browser parses the dom top to bottom and probably alpine is eagerly loading the component before the browser has read the script tag |
Beta Was this translation helpful? Give feedback.
-
I have tried so, but unfortunately I was getting the same error.
The only way I *partially* fixed the etror was by setting up a setTimeout with window.Alpine.initTree(container) in the script tag.
Yes it did fix the issue, but the console was still initially spitting errors and it also keeps spitting errors when I use the component, so I felt like I was onto something, but didnt entirely fix it.
Thanks!
|
Beta Was this translation helpful? Give feedback.
-
by default, HTMX injects dom and then handles the JS, since you can't bulk append script tags. You need to have htmx inject the script tag alone first and then inject the components with alpine. alternatively, use a set timeout would be the lazy way, just use the htmx events. |
Beta Was this translation helpful? Give feedback.
-
Well, after hours of testing the conclusion is that something in htmx 1.9 was broken. Upgrading the library to 2.0 fixed all my problems. Woo! |
Beta Was this translation helpful? Give feedback.
Well, after hours of testing the conclusion is that something in htmx 1.9 was broken. Upgrading the library to 2.0 fixed all my problems. Woo!