index.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <html>
  2. <head>
  3. <script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js" defer></script>
  4. </head>
  5. <body>
  6. <div x-data>
  7. <span x-text="$store.application.name"></span>
  8. <button @click="$store.application.toggle()">Click</button>
  9. </div>
  10. <div x-data>
  11. <code>Persisted</code>
  12. <input type="text" x-model="$store.persisted.example">
  13. <button type="button" @click="$store.persisted.method()">
  14. Call method on persisted store
  15. </button>
  16. </div>
  17. <script src="../dist/spruce.umd.js"></script>
  18. <script>
  19. Spruce.store('application', {
  20. name: 'Application',
  21. toggle() {
  22. this.name = ['Application', 'Example'][Math.floor(Math.random() * 2)]
  23. }
  24. })
  25. Spruce.store('persisted', {
  26. example: 'Hello',
  27. method() {
  28. console.log('test')
  29. }
  30. }, true)
  31. Spruce.watch('application.name', () => {
  32. Spruce.store('persisted').example = 'World';
  33. });
  34. Spruce.watch('persisted.example', () => {
  35. console.log('World!');
  36. })
  37. </script>
  38. </body>
  39. </html>