SimpleCounterDemo.vala 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Spry;
  2. using Inversion;
  3. /**
  4. * SimpleCounterDemo - A basic counter demo for the Actions documentation
  5. *
  6. * Demonstrates:
  7. * - spry-action for same-component actions
  8. * - spry-target for scoped targeting
  9. * - State management via a singleton store
  10. */
  11. public class SimpleCounterDemo : Component {
  12. private CounterDemoStore store = inject<CounterDemoStore>();
  13. public override string markup { get {
  14. return """
  15. <div sid="counter" class="demo-counter">
  16. <div class="counter-display" sid="display">0</div>
  17. <div class="counter-controls">
  18. <button class="counter-btn decrement"
  19. spry-action=":Decrement"
  20. spry-target="counter">−</button>
  21. <button class="counter-btn increment"
  22. spry-action=":Increment"
  23. spry-target="counter">+</button>
  24. </div>
  25. </div>
  26. """;
  27. }}
  28. public override async void prepare() throws Error {
  29. this["display"].text_content = store.count.to_string();
  30. }
  31. public async override void handle_action(string action) throws Error {
  32. switch (action) {
  33. case "Increment":
  34. store.count++;
  35. break;
  36. case "Decrement":
  37. store.count--;
  38. break;
  39. }
  40. }
  41. }
  42. /**
  43. * CounterDemoStore - Singleton store to persist counter state
  44. */
  45. public class CounterDemoStore : Object {
  46. public int count { get; set; default = 0; }
  47. }