Skip to content

Commit

Permalink
Add fix for $parent in when called in events only (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBatdorf authored Sep 21, 2020
1 parent 2698130 commit a6c20bd
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion dist/component.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions example/component.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,13 @@
</div>
<p x-data x-text="$component('tealSquare').color"></p>

<p class="my-4 font-bold">Parent is first encountered inside an event</p>
<div
x-data="{ color: 'pink' }"
class="w-64 h-64 mb-6 flex flex-col items-center justify-center"
:class="`bg-${color}-500`">
<p x-data @click="$parent.color = $parent.color === 'pink' ? 'indigo' : 'pink'">press here</p>
</div>

<script src="http://localhost:35729/livereload.js"></script>
</body>
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alpine-magic-helpers",
"version": "0.3.2",
"version": "0.3.3",
"description": "A set of magic helpers to use with AlpineJS",
"main": "dist/index.js",
"publishConfig": {
Expand Down Expand Up @@ -28,7 +28,7 @@
},
"homepage": "https://github.com/kevinbatdorf/alpine-magic-helpers#readme",
"devDependencies": {
"alpinejs": "^2.4.1",
"alpinejs": "^2.7.0",
"cross-env": "^7.0.2",
"jest": "^26.4.2",
"laravel-mix": "^5.0.5",
Expand Down
5 changes: 1 addition & 4 deletions src/component.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO: These can be refactored some to combine functionality
function registerComponentMagicMethod() {
Alpine.magicProperties.hasOwnProperty('parent') ||
Alpine.addMagicProperty('parent', function ($el) {
Expand Down Expand Up @@ -29,15 +28,13 @@ function registerComponentMagicMethod() {
return
}
})

parentObserver.observe(parentComponent, {
attributes: true,
childList: true,
characterData: true,
subtree: true,
})

return data
return $el.$parent
})

Alpine.magicProperties.hasOwnProperty('component') ||
Expand Down

0 comments on commit a6c20bd

Please sign in to comment.