todos.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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-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.controller('Todos', function (scope, seed) {
  62. // regular properties
  63. scope.todos = todos
  64. scope.filter = 'all'
  65. scope.remaining = todos.reduce(function (count, todo) {
  66. return count + (todo.done ? 0 : 1)
  67. }, 0)
  68. // computed properties
  69. scope.total = function () {
  70. return scope.todos.length
  71. }
  72. scope.completed = function () {
  73. return scope.todos.length - scope.remaining
  74. }
  75. // event handlers
  76. scope.addTodo = function (e) {
  77. var text = e.el.value
  78. if (text) {
  79. e.el.value = ''
  80. scope.todos.push({
  81. text: text,
  82. done: false
  83. })
  84. scope.remaining++
  85. }
  86. }
  87. scope.removeTodo = function (e) {
  88. scope.todos.splice(e.scope.$index, 1)
  89. scope.remaining -= e.scope.done ? 0 : 1
  90. }
  91. scope.toggleTodo = function (e) {
  92. scope.remaining += e.scope.done ? -1 : 1
  93. }
  94. scope.setFilter = function (e) {
  95. scope.filter = e.el.className
  96. }
  97. })
  98. var app = Seed.bootstrap()
  99. </script>
  100. </body>
  101. </html>