x-modelable and FileList issues #4813
Unanswered
JayTennant
asked this question in
1. Help
Replies: 1 comment
|
The ideal way to handle this kind of stuff to use signals so that entangled properties can actually just use the same underlying signal, instead of needing to do this clunky work to keep two reactive values in sync. |
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.
Alpine.js version
v3.15.11
Browser and operating system
Chrome
What do you need help with?
I'm writing some custom tags in CFML to encapsulate some Alpine components.
x-modelableis a perfect use case for this, but it's not working for one. In my custom CFMLfile_selectortag that encapsulates:I have a FileList object that is converted to an array and passed to
addFiles(), which updates the modelable value.This works all fine and good within the Alpine component: I see an array of File objects, and can grab the name, size, type, etc. from them. But the Alpine variable passed in through
x-modelis getting empty File objects.Importantly, File objects do not own enumerable properties, instead, as platform objects, they have Browser provided interfaces (like "name", "size", "type"). In looking through the source, it appears the directive
x-modelableuses theentangle()method that does a JSON round-trip clone of the objects, usingJSON.parse( JSON.stringify( value ) ), see:alpine/packages/alpinejs/src/entangle.js
Line 39 in a0450cc
But JSON clones copy the object's own enumerable properties, so that explains why the File objects are copied to the
x-modelvariable as empty objects.I'm considering a few workarounds to avoid the issues with
x-modelable, including:x-modelattribute added to the custom tag into anx-effect, as in:<cf_file_selector x-model="files" />renders as<div x-data="{...}" x-effect="#attributes["x-model"]# = modelValue">...</div>$dispatch( 'change', { files, errors } )Does anybody else have any thoughts about approaching this? Perhaps could something about this be noted in the docs for
x-modelable? Or perhaps could a change be considered for howentangle()clones the objects (ie.structuredClone()and detecting "plain object JSON vs. other objects")? I realize any changes toentangle()also affect how comparisons are made, so maybe that's too much work.How do you expect it to work?
It'd be nice if File platform objects would work with
x-modelable.Please confirm
All reactions