Created
October 11, 2021 20:10
-
-
Save hasinhayder/468c124921d94ab93bd42b8ea68b9e53 to your computer and use it in GitHub Desktop.
Mutate AlpineJS Object's Data in Different Ways
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Mutating AlpineJS Object Data</title> | |
</head> | |
<body> | |
<button onclick="changeDataByDispatchingEvent()">Mutate Alpine Object's Data Using Event</button> | |
<br> | |
<br> | |
<button onclick="changeDataDirectly()">Mutate Alpine Object's Data Directly</button> | |
<br> | |
<br> | |
<br> | |
<div id="yumyum" x-data="data" @ulala.window='toggle'> | |
<button @click="open = !open">Expand</button> | |
<span x-show="open"> Content... </span> | |
</div> | |
<script src="//unpkg.com/alpinejs" defer></script> | |
<script> | |
const data = { | |
open: false, | |
toggle() { | |
this.open = !this.open | |
} | |
} | |
function changeDataByDispatchingEvent() { | |
window.dispatchEvent(new Event('ulala')); | |
} | |
function changeDataDirectly() { | |
const element =document.getElementById('yumyum'); | |
element._x_dataStack[0].open = !element._x_dataStack[0].open | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment