Replies: 15 comments 11 replies
-
If you console.log the edit: Shouldn't this be a discussion instead of an issue? |
Beta Was this translation helpful? Give feedback.
-
I believe this might be related to the cached |
Beta Was this translation helpful? Give feedback.
-
Is there a way to refresh the cache of refs? |
Beta Was this translation helpful? Give feedback.
-
I don't know why you're editing your initial post to make comments confusing, but following on my previous comment. I made a demo and it seems to be working. I used this code and it logged the expected element. <div x-data="">
<p x-ref="hello"></p>
<button @click="() => {var myVar = 'hello'; console.log($refs[myVar])}">press</button>
</div> Can you put up a demo where it's broken? |
Beta Was this translation helpful? Give feedback.
-
Sorry @KevinBatdorf , did not mean to confuse. I just discovered the actual issue and wanted to update the title. I read your thread in discord and it seems to be the same issue danjac has. Your demo is static (not dynamic) so that is why it works. Here is a demo of what does not work...
|
Beta Was this translation helpful? Give feedback.
-
Oh I missed the binding part of your question. yeah you can't bind x-ref anymore. it was sort of a hack to begin with though I think (although useful). You may need a custom directive to handle that instead for now. Could be a useful feature though. I would start a discussion on it and let people upvote to show demand. |
Beta Was this translation helpful? Give feedback.
-
OK. I will just use your suggestion from discord until I can revisit the issue...
|
Beta Was this translation helpful? Give feedback.
-
Yeah, or something clunky like this would work too: <div x-data="{foo: 'bar'}">
<p x-ref="hey">Hey</p>
<p x-init="() => {
const $root = $el.closest('[x-data]')
$root._x_refs || ($root._x_refs = {})
$root._x_refs[foo] = $el
}"></p>
<button @click="console.log($refs)">press</button>
</div> Which you may be able to abstract somehow. |
Beta Was this translation helpful? Give feedback.
-
THanks! So is it safe to assume other things cannot bind in templates now? Things like :href and :class ? |
Beta Was this translation helpful? Give feedback.
-
I don't think that's accurate at all. That's one of Alpine's core features. I don't think Alpine directives were meant to be bound though. I haven't checked but binding other directives might also not produce the same behavior, like |
Beta Was this translation helpful? Give feedback.
-
Yeah, x-ref isn't really meant to be bound - it needs to be processed BEFORE the data is available to evaluate the binding itself |
Beta Was this translation helpful? Give feedback.
-
@KevinBatdorf @calebporzio this is really unintuative. The documentation indicates that it should work like https://alpinejs.dev/directives/ref
|
Beta Was this translation helpful? Give feedback.
-
@calebporzio I had the same issue until I found this after upgrade to v3. It should be mentioned in upgrade guide (it was working fine in v2) and also mentioned in https://alpinejs.dev/directives/ref that it won't work for dynamically created elements. |
Beta Was this translation helpful? Give feedback.
-
I easily lost an hour debugging why my dynamic <input type='text' :x-ref="`rename-${file.relPath}`"/>
this won't automatically bind to $refs async startRenaming(file) {
this.renaming = {...file};
await this.$nextTick(); // <-- make sure that the changes are flushed to DOM.
this.$root.querySelector(`[x-ref="rename-${file.relPath}"]`).select();
}, |
Beta Was this translation helpful? Give feedback.
-
The explanation of $refs from AlpineJS website need to be updated like example bellow.
|
Beta Was this translation helpful? Give feedback.
-
So I have a setup to display dynamic items loaded via fetch. I use the :x-ref="someRef".
In v3 I can no longer do this. I checked and the main this.$refs in my data() does not reflect any dynamic refs added after page loads.
Beta Was this translation helpful? Give feedback.
All reactions