Skip to content

Commit a6c20bd

Browse files
authored
Add fix for $parent in when called in events only (#13)
1 parent 2698130 commit a6c20bd

6 files changed

Lines changed: 17 additions & 12 deletions

File tree

dist/component.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/component.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,13 @@
5353
</div>
5454
<p x-data x-text="$component('tealSquare').color"></p>
5555

56+
<p class="my-4 font-bold">Parent is first encountered inside an event</p>
57+
<div
58+
x-data="{ color: 'pink' }"
59+
class="w-64 h-64 mb-6 flex flex-col items-center justify-center"
60+
:class="`bg-${color}-500`">
61+
<p x-data @click="$parent.color = $parent.color === 'pink' ? 'indigo' : 'pink'">press here</p>
62+
</div>
63+
5664
<script src="http://localhost:35729/livereload.js"></script>
5765
</body>

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "alpine-magic-helpers",
3-
"version": "0.3.2",
3+
"version": "0.3.3",
44
"description": "A set of magic helpers to use with AlpineJS",
55
"main": "dist/index.js",
66
"publishConfig": {
@@ -28,7 +28,7 @@
2828
},
2929
"homepage": "https://github.com/kevinbatdorf/alpine-magic-helpers#readme",
3030
"devDependencies": {
31-
"alpinejs": "^2.4.1",
31+
"alpinejs": "^2.7.0",
3232
"cross-env": "^7.0.2",
3333
"jest": "^26.4.2",
3434
"laravel-mix": "^5.0.5",

src/component.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// TODO: These can be refactored some to combine functionality
21
function registerComponentMagicMethod() {
32
Alpine.magicProperties.hasOwnProperty('parent') ||
43
Alpine.addMagicProperty('parent', function ($el) {
@@ -29,15 +28,13 @@ function registerComponentMagicMethod() {
2928
return
3029
}
3130
})
32-
3331
parentObserver.observe(parentComponent, {
3432
attributes: true,
3533
childList: true,
3634
characterData: true,
3735
subtree: true,
3836
})
39-
40-
return data
37+
return $el.$parent
4138
})
4239

4340
Alpine.magicProperties.hasOwnProperty('component') ||

0 commit comments

Comments
 (0)