Allow forcing $id
to always generate a fresh id
#4106
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A couple of months ago I made this demo after I got an idea of a way to use
$id
to make a quick and easy form with a dynamic number of fieldsets using the same template.I used v3.13.3 at the time and it worked great! However, recently I needed this functionality on a real world project, so I pulled up the demo and copied over the concept, it didn't work. After some debugging I found that the
$id
call on the button click was no longer creating new ids each time it was clicked.At first I thought this was a bug due to this recent update in Alpine, but then I realized that I was actually relying on a bug in v3.13.3 that just happened to make the demo to work as I wanted. Conceptually, it makes perfect sense that a call to
$id
on the same element would always return the same value, that's exactly what it's for.After realizing my mistake, I added the 'Altered Version' to the demo with a secondary variable in the
x-data
that tracks how many times the button has been clicked. While this works just fine, I was left feeling like it was a shame there wasn't a more simple way to just use$id
to generate a fresh value each time and not have to rely on an extra counter variable with no other purpose. I tried a couple of different variations and landed on adding a third argument to$id
to allow caching to simply be disabled for a specific call.$id
will still function exactly the same unlessfalse
is explicitly passed as the third argument, denoting that a new value should be returned each time it is run.