index.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. <div x-data>
  18. <input type="radio" name="colorScheme" x-model="$store.colorScheme" value="light">
  19. Light
  20. <input type="radio" name="colorScheme" x-model="$store.colorScheme" value="dark">
  21. Dark
  22. <p x-text="$store.colorScheme"></p>
  23. </div>
  24. <div x-data>
  25. <input type="text" x-model="$store.todo.new">
  26. <button @click="$store.todo.add($store.todo.new)">
  27. Add Todo
  28. </button>
  29. <ul>
  30. <template x-for="todo in $store.todo.todos">
  31. <li x-text="todo"></li>
  32. </template>
  33. </ul>
  34. </div>
  35. <div x-data>
  36. <input type="text" x-model="$store.user.name">
  37. </div>
  38. <div x-data>
  39. <input type="checkbox" x-model="$store.toggled.on">
  40. <button @click="$store.toggle('toggled.on')">Toggle checkbox</button>
  41. </div>
  42. <script src="../dist/spruce.umd.js"></script>
  43. <script>
  44. Spruce.store('application', {
  45. name: 'Application',
  46. toggle() {
  47. this.name = ['Application', 'Example'][Math.floor(Math.random() * 2)]
  48. }
  49. })
  50. Spruce.store('persisted', {
  51. example: 'Hello',
  52. method() {
  53. console.log('test')
  54. }
  55. }, true)
  56. Spruce.store('toggled', {
  57. on: true
  58. })
  59. Spruce.store('colorScheme', 'light', true)
  60. Spruce.store('todo', {
  61. new: '',
  62. todos: [],
  63. add(todo) {
  64. this.todos.push(todo)
  65. }
  66. })
  67. Spruce.store('user', {
  68. name: 'Ryan'
  69. })
  70. Spruce.watch('user', () => {
  71. Spruce.store('persisted').example = 'Woop!'
  72. })
  73. Spruce.watch('application.name', () => {
  74. Spruce.store('persisted').example = 'World';
  75. });
  76. Spruce.watch('persisted.example', () => {
  77. console.log('World!');
  78. })
  79. Spruce.starting(function () {
  80. console.log('starting up...')
  81. })
  82. Spruce.started(function () {
  83. console.log('started...')
  84. })
  85. Spruce.watch('todo.todos', function () {
  86. console.log('Added todo')
  87. })
  88. </script>
  89. </body>
  90. </html>