app.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. addTodo: function () {
  62. var value = this.newTodo && this.newTodo.trim();
  63. if (!value) {
  64. return;
  65. }
  66. this.todos.push({ title: value, completed: false });
  67. this.newTodo = '';
  68. },
  69. removeTodo: function (todo) {
  70. this.todos.$remove(todo);
  71. },
  72. editTodo: function (todo) {
  73. this.beforeEditCache = todo.title;
  74. this.editedTodo = todo;
  75. },
  76. doneEdit: function (todo) {
  77. if (!this.editedTodo) {
  78. return;
  79. }
  80. this.editedTodo = null;
  81. todo.title = todo.title.trim();
  82. if (!todo.title) {
  83. this.removeTodo(todo);
  84. }
  85. },
  86. cancelEdit: function (todo) {
  87. this.editedTodo = null;
  88. todo.title = this.beforeEditCache;
  89. },
  90. removeCompleted: function () {
  91. this.todos = filters.active(this.todos);
  92. }
  93. },
  94. // a custom directive to wait for the DOM to be updated
  95. // before focusing on the input field.
  96. // http://vuejs.org/guide/custom-directive.html
  97. directives: {
  98. 'todo-focus': function (value) {
  99. if (!value) {
  100. return;
  101. }
  102. var el = this.el;
  103. Vue.nextTick(function () {
  104. el.focus();
  105. });
  106. }
  107. }
  108. });
  109. })(window);