app.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // methods that implement data logic.
  59. // note there's no DOM manipulation here at all.
  60. methods: {
  61. pluralize: function (n) {
  62. return n === 1 ? 'item' : 'items'
  63. },
  64. addTodo: function () {
  65. var value = this.newTodo && this.newTodo.trim();
  66. if (!value) {
  67. return;
  68. }
  69. this.todos.push({ title: value, completed: false });
  70. this.newTodo = '';
  71. },
  72. removeTodo: function (todo) {
  73. this.todos.splice(this.todos.indexOf(todo), 1);
  74. },
  75. editTodo: function (todo) {
  76. this.beforeEditCache = todo.title;
  77. this.editedTodo = todo;
  78. },
  79. doneEdit: function (todo) {
  80. if (!this.editedTodo) {
  81. return;
  82. }
  83. this.editedTodo = null;
  84. todo.title = todo.title.trim();
  85. if (!todo.title) {
  86. this.removeTodo(todo);
  87. }
  88. },
  89. cancelEdit: function (todo) {
  90. this.editedTodo = null;
  91. todo.title = this.beforeEditCache;
  92. },
  93. removeCompleted: function () {
  94. this.todos = filters.active(this.todos);
  95. }
  96. },
  97. // a custom directive to wait for the DOM to be updated
  98. // before focusing on the input field.
  99. // http://vuejs.org/guide/custom-directive.html
  100. directives: {
  101. 'todo-focus': function (el, value) {
  102. if (value) {
  103. el.focus();
  104. }
  105. }
  106. }
  107. });
  108. })(window);