app.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*global Vue, todoStorage */
  2. (function (exports) {
  3. 'use strict';
  4. var filters = {
  5. all: function (todos) {
  6. return todos;
  7. },
  8. active: function (todos) {
  9. return todos.filter(function (todo) {
  10. return !todo.completed;
  11. });
  12. },
  13. completed: function (todos) {
  14. return todos.filter(function (todo) {
  15. return todo.completed;
  16. });
  17. }
  18. };
  19. exports.app = new Vue({
  20. // app initial state
  21. data: {
  22. todos: todoStorage.fetch(),
  23. newTodo: '',
  24. editedTodo: null,
  25. visibility: 'all'
  26. },
  27. // watch todos change for localStorage persistence
  28. watch: {
  29. todos: {
  30. handler: function (todos) {
  31. todoStorage.save(todos);
  32. },
  33. deep: true
  34. }
  35. },
  36. // computed properties
  37. // http://vuejs.org/guide/computed.html
  38. computed: {
  39. filteredTodos: function () {
  40. return filters[this.visibility](this.todos);
  41. },
  42. remaining: function () {
  43. return filters.active(this.todos).length;
  44. },
  45. allDone: {
  46. get: function () {
  47. return this.remaining === 0;
  48. },
  49. set: function (value) {
  50. this.todos.forEach(function (todo) {
  51. todo.completed = value;
  52. });
  53. }
  54. }
  55. },
  56. filters: {
  57. pluralize: function (n) {
  58. return n === 1 ? 'item' : 'items'
  59. }
  60. },
  61. // methods that implement data logic.
  62. // note there's no DOM manipulation here at all.
  63. methods: {
  64. addTodo: function () {
  65. var value = this.newTodo && this.newTodo.trim();
  66. if (!value) {
  67. return;
  68. }
  69. this.todos.push({
  70. id: todoStorage.uid++,
  71. title: value,
  72. completed: false
  73. });
  74. this.newTodo = '';
  75. },
  76. removeTodo: function (todo) {
  77. this.todos.splice(this.todos.indexOf(todo), 1);
  78. },
  79. editTodo: function (todo) {
  80. this.beforeEditCache = todo.title;
  81. this.editedTodo = todo;
  82. },
  83. doneEdit: function (todo) {
  84. if (!this.editedTodo) {
  85. return;
  86. }
  87. this.editedTodo = null;
  88. todo.title = todo.title.trim();
  89. if (!todo.title) {
  90. this.removeTodo(todo);
  91. }
  92. },
  93. cancelEdit: function (todo) {
  94. this.editedTodo = null;
  95. todo.title = this.beforeEditCache;
  96. },
  97. removeCompleted: function () {
  98. this.todos = filters.active(this.todos);
  99. }
  100. },
  101. // a custom directive to wait for the DOM to be updated
  102. // before focusing on the input field.
  103. // http://vuejs.org/guide/custom-directive.html
  104. directives: {
  105. 'todo-focus': function (el, value) {
  106. if (value) {
  107. el.focus();
  108. }
  109. }
  110. }
  111. });
  112. })(window);