livewire
livewire copied to clipboard
Allow add fallback in @entangle directive / $wire.entangle
Review the contribution guide first at: https://laravel-livewire.com/docs/2.x/contribution-guide
1️⃣ Is this something that is wanted/needed? Did you create a discussion about it first? https://github.com/livewire/livewire/discussions/4182 https://github.com/livewire/livewire/discussions/4856 2️⃣ Did you create a branch for your fix/feature? (Master branch PR's will be closed) Yes 3️⃣ Does it contain multiple, unrelated changes? Please separate the PRs out. No 4️⃣ Does it include tests? (Required) Yes 5️⃣ Please include a thorough description (including small code snippets if possible) of the improvement and reasons why it's useful.
Sometimes we need to put a default value in a property that is called with entangle on the front-end instead of creating a logic to add via the back-end
Currently an error is expected when I try to do this:
public array $items = [];
@for($i=0;$i<5;$i++)
<div x-data="{ items: @entangle('items.'.$i) }">
<div x-text="items"></div>
</div>
@endfor

This PR adds the default value if the property is not found in the index
This PR does not change the current behavior
@for($i=0;$i<5;$i++)
<div x-data="{ items: @entangle('items.'.$i, {{ $i }}) }">
<div x-text="items"></div>
</div>
@endfor
Items: @json($items)
Result:
<div x-data="{ items: window.Livewire.find('...').entangle('items.0', 1) }">
<div x-text="items"></div>
</div>
<div x-data="{ items: window.Livewire.find('...').entangle('items.1', 2) }">
<div x-text="items"></div>
</div>
// ...
Items: {"items":[0,1,2,3,4]}
Thanks for contributing! 🙌
@luanfreitasdev thanks for the PR! Sorry for the delay in responding. Caleb is currently off on paternity leave, so it might be a little while before he reviews it 🙂
Thanks @joshhanley 👍
While we're developing V3, we're trying to keep V2 stable and limit API changes/additions. In that spirit, PR's like this that may otherwise make sense to merge, we're unfortunately closing. Thank for your effort and input!