Replies: 2 comments 4 replies
-
|
Things will work better if you morph from an x-data. It does seem like this should be supported though. (On a side note, x-ref is usually a bad idea) |
Beta Was this translation helpful? Give feedback.
4 replies
-
|
#4744 Have a PR up to fix this issue with alpine morph |
Beta Was this translation helpful? Give feedback.
0 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.
Uh oh!
There was an error while loading. Please reload this page.
-
I was working in a project with HTMX and Alpine and found that when I tried to morph an HTML fragment that contained an x-ref directive but didn't have an overall x-data parent, x-ref would crash.
CodePen with example: https://codepen.io/elied/pen/rNoQRPr
The error message is: Uncaught TypeError: Cannot read properties of undefined (reading '_x_refs')
And comes from this line in x_ref.js:
if (! root._x_refs) root._x_refs = {}The problem is that
rootis undefined, since the HTML fragment doesn't contain an x-data directive.I think this is a bug since I would expect to be able to morph in pieces of an Alpine component without needing to morph the entire thing.
I read through the morph code a bit and I understand that we need to evaluate the Alpine directives on the new fragment so that we can accurately compare the two trees. However, there is handling like this:
Which makes me think that this is intended to be supported, and is maybe just a matter of copying over _x_refs onto toEl, similar to how we handle _x_dataStack?
It's also fine if this is just a limitation of morph, but any guidance or suggestions like "In general, things will work better if you always morph from a top-level Alpine component" would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions