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

Using the parent variable after nested components #21

Closed
keyurshah opened this issue Dec 12, 2019 · 2 comments
Closed

Using the parent variable after nested components #21

keyurshah opened this issue Dec 12, 2019 · 2 comments

Comments

@keyurshah
Copy link
Contributor

I'm have a component and then declaring nested components inside.

Once I use a data variable after a new nested component is declared, the variable doesn't work. Here's an example:

https://codepen.io/kjshah/pen/df81e5331e12140ac50ecb342dedfb61

<div x-data="values()">
  <div class="font-semibold">This Doesn't Work</div>
  
  <div x-data="{name: 'blue widget'}">
    Name: <span x-text='name'></span>
  </div>
  
  <hr>
  (total should show here)
  Total: <span x-text="total"></span>
</div>
  
--------------------------------------------------
  
<div x-data="values()">
  <div class="font-semibold">This Works</div>
  
  Total: <span x-text="total"></span>
  
  <div x-data="{name: 'blue widget'}">
    Name: <span x-text='name'></span>
  </div>
</div>
<script>
function values() {
  return {
    total: 1,
  }
}
</script>
@calebporzio
Copy link
Collaborator

Good catch, fixed in v0.4.3

@rosswintle
Copy link

Hi folks,

I've been playing with Alpine this evening for the first time and I THINK this is still an issue.

For starters, it's not documented whether referring to a variable in a parent scope is even possible, so I assumed it was. This should be made clear in the readme. It's entirely possible that referring to data declared higher up in the DOM is not allowed. It's also entirely possible that I just couldn't find where in the readme it tells me this.

If it is possible then it's still an issue as far as I can tell.

Example:

I have a collection of buttons and I want to apply a class to a button when that button is clicked.

Here's a simple example coded with button values hard-coded: https://codepen.io/magicroundabout/pen/jOEGXrd

This works and is fine. But I figured I could set the button ID as data on the button and use it in the directives, like this: https://codepen.io/magicroundabout/pen/KKwXbME

But in the second example here the button can't see either the data declared higher up in the DOM or its own data!

Here's a CodePen with both examples together: https://codepen.io/magicroundabout/pen/qBEPLqq

Is this a bug? Or a misunderstanding?

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

No branches or pull requests

3 participants