todos.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Todo</title>
  5. <meta charset="utf-8">
  6. <script src="../dist/seed.js"></script>
  7. <style type="text/css">
  8. .red {
  9. color: red;
  10. }
  11. .done {
  12. text-decoration: line-through;
  13. }
  14. #app.all .all {
  15. font-weight: bold;
  16. }
  17. #app.remaining .todo.done {
  18. display: none;
  19. }
  20. #app.remaining .remaining {
  21. font-weight: bold;
  22. }
  23. #app.completed .todo:not(.done) {
  24. display: none;
  25. }
  26. #app.completed .completed {
  27. font-weight: bold;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="app" sd-controller="Todos" sd-data="test" sd-class="filter">
  33. <div>
  34. <input placeholder="What needs to be done?" sd-on="change:addTodo">
  35. </div>
  36. <ul sd-show="todos">
  37. <li class="todo" sd-each="todo:todos" sd-class="done:todo.done">
  38. <input type="checkbox" sd-checked="todo.done" sd-on="change:toggleTodo">
  39. <span sd-text="todo.text"></span>
  40. <a sd-on="click:removeTodo">X</a>
  41. </li>
  42. </ul>
  43. <div id="footer">
  44. Total: <span sd-text="total < todos"></span> |
  45. Remaining: <span sd-text="remaining"></span> |
  46. Completed: <span sd-text="completed < remaining total"></span>
  47. <br>
  48. <a class="all" sd-on="click:setFilter">Show All</a> |
  49. <a class="remaining" sd-on="click:setFilter">Show Remaining</a> |
  50. <a class="completed" sd-on="click:setFilter">Show Completed</a>
  51. </div>
  52. </div>
  53. <script>
  54. var Seed = require('seed')
  55. var todos = [
  56. { text: 'make nesting controllers work', done: true },
  57. { text: 'complete ArrayWatcher', done: false },
  58. { text: 'computed properties', done: false },
  59. { text: 'parse textnodes', done: false }
  60. ]
  61. Seed.data('test', { todos: todos })
  62. Seed.controller('Todos', function (scope, seed) {
  63. // regular properties
  64. // scope.todos = todos
  65. scope.filter = 'all'
  66. scope.remaining = todos.reduce(function (count, todo) {
  67. return count + (todo.done ? 0 : 1)
  68. }, 0)
  69. // computed properties
  70. scope.total = function () {
  71. return scope.todos.length
  72. }
  73. scope.completed = function () {
  74. return scope.todos.length - scope.remaining
  75. }
  76. // event handlers
  77. scope.addTodo = function (e) {
  78. var text = e.el.value
  79. if (text) {
  80. e.el.value = ''
  81. scope.todos.push({
  82. text: text,
  83. done: false
  84. })
  85. scope.remaining++
  86. }
  87. }
  88. scope.removeTodo = function (e) {
  89. scope.todos.splice(e.seed.index, 1)
  90. scope.remaining -= e.seed.scope.done ? 0 : 1
  91. }
  92. scope.toggleTodo = function (e) {
  93. scope.remaining += e.seed.scope.done ? -1 : 1
  94. }
  95. scope.setFilter = function (e) {
  96. scope.filter = e.el.className
  97. }
  98. })
  99. Seed.bootstrap()
  100. </script>
  101. </body>
  102. </html>