Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested x-for loop scoping #316

Merged
merged 1 commit into from
Mar 30, 2020
Merged

Conversation

calebporzio
Copy link
Collaborator

This is now possible:

<div x-data="{ foos: [{bars: [{bobs: ['one', 'two']}, {bobs: ['three', 'four']}]}, {bars: [{bobs: ['five', 'six']}, {bobs: ['seven', 'eight']}]}] }">
    <template x-for="foo in foos">
        <div>
            <template x-for="bar in foo.bars">
                <div>
                    <template x-for="bob in bar.bobs">
                        <span x-text="bob"></span>
                    </template>
                </div>
            </template>
        </div>
    </template>
</div>

(This outputs:)
image

@calebporzio calebporzio mentioned this pull request Mar 30, 2020
@calebporzio calebporzio merged commit 5fe4cbf into master Mar 30, 2020
@husayt
Copy link

husayt commented Apr 10, 2020

@calebporzio How do you access foo or bar inside the last x-for?

@husayt
Copy link

husayt commented Apr 10, 2020

seems my last question is not supported yet and waiting for this to merge

@timohoelzer
Copy link

timohoelzer commented Aug 12, 2020

Any updates on nesting.

Try to do something like the following which seams a practical case to me.
https://codepen.io/tatundkraft/pen/4f34ad6768ec85652a57939b3cbf6572?editors=1001

Creating a new x-data within x-for makes foo unreachable.

Am I missing something?

Many thanks in advance.

@SimoTod
Copy link
Collaborator

SimoTod commented Aug 12, 2020

That use case is not supported. Nested components cannot communicate with the parent components.
The fact that a component is inside an x-for is not relevant, you would get a similar error with

<div x-data="{product: { name:'Product 1', images:[1,2] }]">
    <div x-data="product">
        ...
    </div>
</div>

As long as you define a new x-data attribute, that component creates its own standalone scope. You can have a look at spruce if you need your components to share a common state

@calebporzio calebporzio deleted the support-nested-for-loops branch June 11, 2021 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants