Comments (13)
Hi @markfirmware. I think there's some confusion about how Alpine works (also considering the other issues you opened on the official repo).
<p x-text="foo"></p> <!-- this one by itself doesn't work, all alpine directives need to be wrapped in a component (x-data) -->
<div x-data="{foo: 'bar'}">
<p x-text="foo"></p> <!-- this one works, default alpine behaviour -->
</div>
<div x-data="{foo: 'bar'}">
<div x-data="{baz: 'bob'}">
<p x-text="foo"></p> <!-- this one does not work, alpine can't see anything outside the component (element with x-data) and it doesn't have the concept of inheriting data from parent components -->
</div>
</div>
<!-- but, using the magic helpers -->
<div x-data="{foo: 'bar'}">
<div x-data="{baz: 'bob'}">
<p x-text="$parent.foo"></p> <!-- this one works -->
</div>
</div>
<div x-data="{foo: 'bar'}">
...
</div>
<div x-data="{baz: 'bob'}">
<p x-text="foo"></p> <!-- again, this one doesn't work, the only field you can see here out of the box is baz -->
</div>
<div x-id="mycomponent" x-data="{foo: "bar"}">
...
</div>
<div x-data="{baz: 'bob'}" >
<p x-text="$component('mycomponent').foo"></p> <!-- using magic helpers, this one works-->
</div>
I hope it makes things clearer. In both repo there are discussion tabs where you can ask questions, we tend to use those ones and only use issues for actual bugs. There is also a discord server you can join where other people can help with basic questions.
from alpine-magic-helpers.
Ok. I will let you close this or manage it as you see fit. See you in the discussions. Mark.
from alpine-magic-helpers.
The second example is a bit more complex.
Basically, you have 3 components and you built a circular reference between them.
From component 1 you refer component 3 and from component 3 you refer component 1. At that point, when you click on 1 and 3, the system start updates the component forever and it eventually freeze.
Not related to the reported bug but using events to communicate between components would probably be easier.
Also, if your goal is to create a select2 clone, maybe you can create a single component with all the required logic already incapsulated.
from alpine-magic-helpers.
$component()
is to be used to accessing properties from outside of its own component scope, so for this you would just use x
.
Can you make a Codepen showing your issue with the error being reported?
from alpine-magic-helpers.
$component()
is to be used to accessing properties from outside of its own component scope, so for this you would just usex
.Can you make a Codepen showing your issue with the error being reported?
https://codepen.io/markfirmware/pen/poEdqRz
from alpine-magic-helpers.
The codepen works as expected. Could you please describe what the issue exactly is and how to reproduce it?
As a separate note, you don't need to use $component if you are already inside the scope of the right component. x-text="x"
works just fine and it's more performant.
from alpine-magic-helpers.
I am using firefox on a debian digital ocean droplet. The codepen indicates script trouble that needs to stop or wait. Let me see if a fresh droplet or linode can reproduce.
from alpine-magic-helpers.
Thanks, I tried forefox and it actually freezes.
As we said at the beginning, you shouldn't use $component if you are already inside the right component context but we'll see if we can fix it or, at least, warn the user about circular references.
from alpine-magic-helpers.
No worries.
Note for the team. Regression happened in 4ef9ed6...f9e4a3b.
It seemed to work correctly before that refactoring.
from alpine-magic-helpers.
We could probably easily add a check if ($el === component.__x.$el) then bail
but I'm curious whether there's an opportunity for some optimization though
from alpine-magic-helpers.
<div
x-id="order_1"
x-data="{
input:'one select value',
click(evt) {
$component('selectApline').clickSelect('order_1', evt, [{'id':1,'name':'one'},{'id':2,'name':'two'},{'id':3,'name':'three'}]);
}
}"
x-on:click='click($event.target)'>
<span x-text="input"></span>
</div>
<div
x-id="order_2"
x-data="{
input:'two select value',
click(evt) {
$component('selectApline').clickSelect('order_2', evt, [{'id':1,'name':'111'},{'id':2,'name':'222'},{'id':3,'name':'333'}]);
}
}"
x-on:click='click($event.target)'>
<span x-text="input"></span>
</div>
<div
x-id="selectApline"
x-data="select({})">
<template x-for="(it, index) in result" :key="index">
<span x-text="it.name"
x-on:click="selected(`${it.name}`)"></span>
</template>
</div>
<script>
function select(config) {
return {
result: [],
name: null,
selected: function (id) {
console.log(this.$component(`${this.name}`).input);
this.$component(`${this.name}`).input = id
console.log(`selected`);
},
clickSelect: function (n, t, d) {
console.log(n);
console.log(t);
console.log(d);
this.name = n
this.target = t
this.result = d
console.log(`click`);
},
}
}
</script>
I created another
Thus, the css problem was solved. Almost everything has been resolved.
But. Last ! There was a problem at the end.
selected: in function (id)
console.log(this.$component(${this.name}).input);
this.$component(${this.name}).input = id
Calling another x-id via this.$component takes freezing.
Freezing takes place just by calling.
However, it appears well on the console, and even the value actually changes.
The moment each other refers to each other through $component
data-last-refresh keeps rising.
Probably the same as the above issue...
Can you possibly solve this problem?
My only hope disappears.
You need to implement select2, but due to css problem, the area does not come out because of overflow, like a real select2 plugin
I tried to work by subtracting the selector list box to the outside (x-data) and connecting it there.
Is it impossible to make it like this?
To solve the overflow css problem of select list (div)...
Like select2, the select list area should be placed at the end of the .
from alpine-magic-helpers.
That's right, but due to the css problem, I had to open the select listbox at the end of the body like select2.
However, I solved the css problem and made it a single component.
Now you know that there is also a way to do it as an event when component 1 communicates to component 3!
Thank you :)
from alpine-magic-helpers.
I don't think we need to be supporting circular references as that sort of defeats the purpose of using $component
and adds way too much overhead that shouldn't be there. ie. it's better we let this break.
A PR to show a warning would be nice though if anyone wants to add some detection.
from alpine-magic-helpers.
Related Issues (20)
- $get and $post are not defined HOT 2
- autoIntervalTest in $interval doesn't resume/restart properly HOT 11
- Test failing with latest Alpine version HOT 3
- Using $parent in for loop data that has been fetched (to be eventually used in an interval) HOT 9
- iterator not working in table? HOT 2
- $parent not working with x-for HOT 6
- Problem using $scroll with $watch
- Hi Support AlpineJS V3 alpine-collective..? HOT 16
- RFC: Adding a $select magic helper HOT 4
- Scroll not working when loaded from another livewire component HOT 3
- Seems like $scroll smooth is not smooth anymore? HOT 4
- Missing source of script tag in installation-guide? HOT 1
- scroll not working in alpine.js v3 HOT 1
- $scroll produces promise error HOT 2
- [Question] Finding unused translations HOT 1
- Fallback for missing translations HOT 4
- Installation suggestion is incorrect
- [V3] Installation suggestion incorrect? HOT 1
- Cannot install HOT 1
- Installing doesn't work in V3 – callback is not a function HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alpine-magic-helpers.