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