Skip to content

Commit ef97107

Browse files
feat(alpine): add context Alpine.store() example (#316)
Co-authored-by: Mathieu Schimmerling <mathieu.schimmerling@protonmail.com>
1 parent d91d4e2 commit ef97107

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ How do we solve this ? Developers love having framework overview by examples. It
434434
<summary>
435435
<img width="18" height="18" src="static/framework/alpine.svg" />
436436
<b>Alpine</b>
437-
<img src="https://us-central1-progress-markdown.cloudfunctions.net/progress/96" />
437+
<img src="https://us-central1-progress-markdown.cloudfunctions.net/progress/100" />
438438
</summary>
439439

440440
- [x] Reactivity
@@ -451,12 +451,12 @@ How do we solve this ? Developers love having framework overview by examples. It
451451
- [x] Lifecycle
452452
- [x] On mount
453453
- [x] On unmount
454-
- [ ] Component composition
454+
- [x] Component composition
455455
- [x] Props
456456
- [x] Emit to parent
457457
- [x] Slot
458458
- [x] Slot fallback
459-
- [ ] Context
459+
- [x] Context
460460
- [x] Form input
461461
- [x] Input text
462462
- [x] Checkbox
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!-- x-text must be called on or inside an x-data block. -->
2+
<!-- The two could have a shared parent with x-data, or each could individually have x-data like so: -->
3+
4+
<h1 x-data>
5+
Welcome back, <span x-text="$store.user.username">unknown user</span>
6+
</h1>
7+
8+
<div x-data>
9+
<h2>My Profile</h2>
10+
<p>Username: <span x-text="$store.user.username"></span></p>
11+
<p>Email: <span x-text="$store.user.email"></span></p>
12+
<button @click="$store.user.updateUsername('Jane')">
13+
Update username to Jane
14+
</button>
15+
</div>
16+
17+
<script>
18+
document.addEventListener("alpine:init", () => {
19+
Alpine.store("user", {
20+
id: 1,
21+
username: "unicorn42",
22+
email: "unicorn42@example.com",
23+
24+
updateUsername(newUsername) {
25+
this.username = newUsername;
26+
},
27+
});
28+
});
29+
</script>

0 commit comments

Comments
 (0)