mutations.js 780 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. export const STORAGE_KEY = 'todos-vuejs'
  2. // for testing
  3. if (navigator.userAgent.indexOf('PhantomJS') > -1) {
  4. window.localStorage.clear()
  5. }
  6. export const state = {
  7. todos: JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]')
  8. }
  9. export const mutations = {
  10. addTodo (state, { text }) {
  11. state.todos.push({
  12. text,
  13. done: false
  14. })
  15. },
  16. deleteTodo (state, { todo }) {
  17. state.todos.splice(state.todos.indexOf(todo), 1)
  18. },
  19. toggleTodo (state, { todo }) {
  20. todo.done = !todo.done
  21. },
  22. editTodo (state, { todo, value }) {
  23. todo.text = value
  24. },
  25. toggleAll (state, { done }) {
  26. state.todos.forEach((todo) => {
  27. todo.done = done
  28. })
  29. },
  30. clearCompleted (state) {
  31. state.todos = state.todos.filter(todo => !todo.done)
  32. }
  33. }