app.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var Seed = require('seed')
  2. var todos = [
  3. { text: 'make nesting Objects work', done: false },
  4. { text: 'auto dependency extraction', done: true },
  5. { text: 'computed properties', done: true }
  6. ]
  7. Seed.controller('Todos', function (scope) {
  8. // regular properties -----------------------------------------------------
  9. scope.todos = todos
  10. scope.filter = window.location.hash.slice(2) || 'all'
  11. scope.remaining = todos.reduce(function (count, todo) {
  12. return count + (todo.done ? 0 : 1)
  13. }, 0)
  14. // computed properties ----------------------------------------------------
  15. scope.total = {get: function () {
  16. return scope.todos.length
  17. }}
  18. scope.completed = {get: function () {
  19. return scope.total - scope.remaining
  20. }}
  21. scope.itemLabel = {get: function () {
  22. return scope.remaining > 1 ? 'items' : 'item'
  23. }}
  24. scope.allDone = {
  25. get: function () {
  26. return scope.remaining === 0
  27. },
  28. set: function (value) {
  29. scope.todos.forEach(function (todo) {
  30. todo.done = value
  31. })
  32. scope.remaining = value ? 0 : scope.total
  33. }
  34. }
  35. // event handlers ---------------------------------------------------------
  36. scope.addTodo = function (e) {
  37. if (e.el.value) {
  38. scope.todos.unshift({ text: e.el.value })
  39. e.el.value = ''
  40. scope.remaining++
  41. }
  42. }
  43. scope.removeTodo = function (e) {
  44. scope.todos.remove(e.scope)
  45. scope.remaining -= e.scope.done ? 0 : 1
  46. }
  47. scope.updateCount = function (e) {
  48. scope.remaining += e.scope.done ? -1 : 1
  49. }
  50. scope.edit = function (e) {
  51. e.scope.editing = true
  52. }
  53. scope.stopEdit = function (e) {
  54. e.scope.editing = false
  55. }
  56. scope.setFilter = function (e) {
  57. scope.filter = e.el.dataset.filter
  58. }
  59. scope.removeCompleted = function () {
  60. if (scope.completed === 0) return
  61. scope.todos = scope.todos.filter(function (todo) {
  62. return !todo.done
  63. })
  64. }
  65. })
  66. Seed.bootstrap()