index.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. <script src="../dist/spruce.umd.js"></script>
  39. <script>
  40. Spruce.store('application', {
  41. name: 'Application',
  42. toggle() {
  43. this.name = ['Application', 'Example'][Math.floor(Math.random() * 2)]
  44. }
  45. })
  46. Spruce.store('persisted', {
  47. example: 'Hello',
  48. method() {
  49. console.log('test')
  50. }
  51. }, true)
  52. Spruce.store('colorScheme', 'light', true)
  53. Spruce.store('todo', {
  54. new: '',
  55. todos: [],
  56. add(todo) {
  57. this.todos.push(todo)
  58. }
  59. })
  60. Spruce.store('user', {
  61. name: 'Ryan'
  62. })
  63. Spruce.watch('user', () => {
  64. Spruce.store('persisted').example = 'Woop!'
  65. })
  66. Spruce.watch('application.name', () => {
  67. Spruce.store('persisted').example = 'World';
  68. });
  69. Spruce.watch('persisted.example', () => {
  70. console.log('World!');
  71. })
  72. Spruce.starting(function () {
  73. console.log('starting up...')
  74. })
  75. Spruce.started(function () {
  76. console.log('started...')
  77. })
  78. Spruce.watch('todo.todos', function () {
  79. console.log('Added todo')
  80. })
  81. </script>
  82. </body>
  83. </html>