1
0

store.js 901 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import Vue from 'vue'
  2. import Vuex from '../../../src'
  3. import middlewares from './middlewares'
  4. Vue.use(Vuex)
  5. export const STORAGE_KEY = 'todos-vuejs'
  6. // for testing
  7. if (navigator.userAgent.indexOf('PhantomJS') > -1) {
  8. localStorage.clear()
  9. }
  10. const state = {
  11. todos: JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')
  12. }
  13. const mutations = {
  14. ADD_TODO (state, text) {
  15. state.todos.push({
  16. text: text,
  17. done: false
  18. })
  19. },
  20. DELETE_TODO (state, todo) {
  21. state.todos.$remove(todo)
  22. },
  23. TOGGLE_TODO (state, todo) {
  24. todo.done = !todo.done
  25. },
  26. EDIT_TODO (state, todo, text) {
  27. todo.text = text
  28. },
  29. TOGGLE_ALL (state, done) {
  30. state.todos.forEach((todo) => {
  31. todo.done = done
  32. })
  33. },
  34. CLEAR_COMPLETED (state) {
  35. state.todos = state.todos.filter(todo => !todo.done)
  36. }
  37. }
  38. export default new Vuex.Store({
  39. state,
  40. mutations,
  41. middlewares
  42. })