瀏覽代碼

tweak todomvc example

Evan You 8 年之前
父節點
當前提交
9fe44ae1b4

+ 3 - 3
examples/todomvc/components/App.vue

@@ -44,7 +44,7 @@
 </template>
 </template>
 
 
 <script>
 <script>
-import { mapActions } from 'vuex'
+import { mapMutations } from 'vuex'
 import Todo from './Todo.vue'
 import Todo from './Todo.vue'
 
 
 const filters = {
 const filters = {
@@ -79,11 +79,11 @@ export default {
     addTodo (e) {
     addTodo (e) {
       var text = e.target.value
       var text = e.target.value
       if (text.trim()) {
       if (text.trim()) {
-        this.$store.dispatch('addTodo', { text })
+        this.$store.commit('addTodo', { text })
       }
       }
       e.target.value = ''
       e.target.value = ''
     },
     },
-    ...mapActions([
+    ...mapMutations([
       'toggleAll',
       'toggleAll',
       'clearCompleted'
       'clearCompleted'
     ])
     ])

+ 2 - 2
examples/todomvc/components/Todo.vue

@@ -19,7 +19,7 @@
 </template>
 </template>
 
 
 <script>
 <script>
-import { mapActions } from 'vuex'
+import { mapMutations } from 'vuex'
 
 
 export default {
 export default {
   name: 'Todo',
   name: 'Todo',
@@ -39,7 +39,7 @@ export default {
     }
     }
   },
   },
   methods: {
   methods: {
-    ...mapActions([
+    ...mapMutations([
       'editTodo',
       'editTodo',
       'toggleTodo',
       'toggleTodo',
       'deleteTodo'
       'deleteTodo'

+ 0 - 10
examples/todomvc/store/actions.js

@@ -1,10 +0,0 @@
-export const addTodo = makeAction('ADD_TODO')
-export const deleteTodo = makeAction('DELETE_TODO')
-export const toggleTodo = makeAction('TOGGLE_TODO')
-export const editTodo = makeAction('EDIT_TODO')
-export const toggleAll = makeAction('TOGGLE_ALL')
-export const clearCompleted = makeAction('CLEAR_COMPLETED')
-
-function makeAction (type) {
-  return ({ commit }, payload) => commit(type, payload)
-}

+ 1 - 44
examples/todomvc/store/index.js

@@ -1,55 +1,12 @@
 import Vue from 'vue'
 import Vue from 'vue'
 import Vuex from 'vuex'
 import Vuex from 'vuex'
+import { state, mutations } from './mutations'
 import plugins from './plugins'
 import plugins from './plugins'
-import * as actions from './actions'
 
 
 Vue.use(Vuex)
 Vue.use(Vuex)
 
 
-export const STORAGE_KEY = 'todos-vuejs'
-
-// for testing
-if (navigator.userAgent.indexOf('PhantomJS') > -1) {
-  localStorage.clear()
-}
-
-const state = {
-  todos: JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')
-}
-
-const mutations = {
-  ADD_TODO (state, { text }) {
-    state.todos.push({
-      text,
-      done: false
-    })
-  },
-
-  DELETE_TODO (state, { todo }) {
-    state.todos.splice(state.todos.indexOf(todo), 1)
-  },
-
-  TOGGLE_TODO (state, { todo }) {
-    todo.done = !todo.done
-  },
-
-  EDIT_TODO (state, { todo, value }) {
-    todo.text = value
-  },
-
-  TOGGLE_ALL (state, { done }) {
-    state.todos.forEach((todo) => {
-      todo.done = done
-    })
-  },
-
-  CLEAR_COMPLETED (state) {
-    state.todos = state.todos.filter(todo => !todo.done)
-  }
-}
-
 export default new Vuex.Store({
 export default new Vuex.Store({
   state,
   state,
-  actions,
   mutations,
   mutations,
   plugins
   plugins
 })
 })

+ 41 - 0
examples/todomvc/store/mutations.js

@@ -0,0 +1,41 @@
+export const STORAGE_KEY = 'todos-vuejs'
+
+// for testing
+if (navigator.userAgent.indexOf('PhantomJS') > -1) {
+  window.localStorage.clear()
+}
+
+export const state = {
+  todos: JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]')
+}
+
+export const mutations = {
+  addTodo (state, { text }) {
+    state.todos.push({
+      text,
+      done: false
+    })
+  },
+
+  deleteTodo (state, { todo }) {
+    state.todos.splice(state.todos.indexOf(todo), 1)
+  },
+
+  toggleTodo (state, { todo }) {
+    todo.done = !todo.done
+  },
+
+  editTodo (state, { todo, value }) {
+    todo.text = value
+  },
+
+  toggleAll (state, { done }) {
+    state.todos.forEach((todo) => {
+      todo.done = done
+    })
+  },
+
+  clearCompleted (state) {
+    state.todos = state.todos.filter(todo => !todo.done)
+  }
+}

+ 2 - 2
examples/todomvc/store/plugins.js

@@ -1,9 +1,9 @@
-import { STORAGE_KEY } from './index'
+import { STORAGE_KEY } from './mutations'
 import createLogger from '../../../src/plugins/logger'
 import createLogger from '../../../src/plugins/logger'
 
 
 const localStoragePlugin = store => {
 const localStoragePlugin = store => {
   store.subscribe((mutation, { todos }) => {
   store.subscribe((mutation, { todos }) => {
-    localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
+    window.localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
   })
   })
 }
 }