app.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var storageKey = 'todos-seedjs',
  2. storedData = JSON.parse(localStorage.getItem(storageKey))
  3. Seed.controller('Todos', function (scope) {
  4. // regular properties -----------------------------------------------------
  5. scope.todos = Array.isArray(storedData) ? storedData : []
  6. scope.filter = location.hash.slice(2) || 'all'
  7. scope.remaining = scope.todos.reduce(function (n, todo) {
  8. return n + (todo.completed ? 0 : 1)
  9. }, 0)
  10. // computed properties ----------------------------------------------------
  11. scope.total = {get: function () {
  12. return scope.todos.length
  13. }}
  14. scope.completed = {get: function () {
  15. return scope.total - scope.remaining
  16. }}
  17. scope.itemLabel = {get: function () {
  18. return scope.remaining > 1 ? 'items' : 'item'
  19. }}
  20. scope.allDone = {
  21. get: function () {
  22. return scope.remaining === 0
  23. },
  24. set: function (value) {
  25. scope.todos.forEach(function (todo) {
  26. todo.completed = value
  27. })
  28. scope.remaining = value ? 0 : scope.total
  29. }
  30. }
  31. // event handlers ---------------------------------------------------------
  32. scope.addTodo = function (e) {
  33. if (e.el.value) {
  34. scope.todos.unshift({ title: e.el.value, completed: false })
  35. e.el.value = ''
  36. scope.remaining++
  37. }
  38. }
  39. scope.removeTodo = function (e) {
  40. scope.todos.remove(e.scope)
  41. scope.remaining -= e.scope.completed ? 0 : 1
  42. }
  43. scope.updateCount = function (e) {
  44. scope.remaining += e.scope.completed ? -1 : 1
  45. }
  46. scope.edit = function (e) {
  47. e.scope.editing = true
  48. }
  49. scope.stopEdit = function (e) {
  50. e.scope.editing = false
  51. }
  52. scope.removeCompleted = function () {
  53. if (scope.completed === 0) return
  54. scope.todos = scope.todos.filter(function (todo) {
  55. return !todo.completed
  56. })
  57. }
  58. // listen for hash change
  59. window.addEventListener('hashchange', function () {
  60. scope.filter = location.hash.slice(2)
  61. })
  62. // persist data on leave
  63. window.addEventListener('beforeunload', function () {
  64. localStorage.setItem(storageKey, scope.$serialize('todos'))
  65. })
  66. })
  67. Seed.bootstrap({ debug: true })