app.js 3.4 KB

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