1
0
Эх сурвалжийг харах

update todomvc example to use new API

Evan You 9 жил өмнө
parent
commit
a057dc05be

+ 18 - 12
examples/todomvc/components/App.vue

@@ -9,7 +9,7 @@
         autofocus
         autofocus
         autocomplete="off"
         autocomplete="off"
         placeholder="What needs to be done?"
         placeholder="What needs to be done?"
-        @keyup.enter="addTodo">
+        @keyup.enter="tryAddTodo">
     </header>
     </header>
     <!-- main section -->
     <!-- main section -->
     <section class="main" v-show="todos.length">
     <section class="main" v-show="todos.length">
@@ -47,6 +47,11 @@
 
 
 <script>
 <script>
 import Todo from './Todo.vue'
 import Todo from './Todo.vue'
+import {
+  addTodo,
+  toggleAll,
+  clearCompleted
+} from '../store/actions'
 
 
 const filters = {
 const filters = {
   all: (todos) => todos,
   all: (todos) => todos,
@@ -56,6 +61,16 @@ const filters = {
 
 
 export default {
 export default {
   components: { Todo },
   components: { Todo },
+  vuex: {
+    state: {
+      todos: state => state.todos
+    },
+    actions: {
+      addTodo,
+      toggleAll,
+      clearCompleted
+    }
+  },
   data () {
   data () {
     return {
     return {
       visibility: 'all',
       visibility: 'all',
@@ -63,9 +78,6 @@ export default {
     }
     }
   },
   },
   computed: {
   computed: {
-    todos () {
-      return this.$store.state.todos
-    },
     allChecked () {
     allChecked () {
       return this.todos.every(todo => todo.done)
       return this.todos.every(todo => todo.done)
     },
     },
@@ -77,18 +89,12 @@ export default {
     }
     }
   },
   },
   methods: {
   methods: {
-    addTodo (e) {
+    tryAddTodo (e) {
       var text = e.target.value
       var text = e.target.value
       if (text.trim()) {
       if (text.trim()) {
-        this.$store.actions.addTodo(text)
+        this.addTodo(text)
       }
       }
       e.target.value = ''
       e.target.value = ''
-    },
-    toggleAll () {
-      this.$store.actions.toggleAll()
-    },
-    clearCompleted () {
-      this.$store.actions.clearCompleted()
     }
     }
   }
   }
 }
 }

+ 13 - 6
examples/todomvc/components/Todo.vue

@@ -19,8 +19,21 @@
 </template>
 </template>
 
 
 <script>
 <script>
+import {
+  toggleTodo,
+  deleteTodo,
+  editTodo
+} from '../store/actions'
+
 export default {
 export default {
   props: ['todo'],
   props: ['todo'],
+  vuex: {
+    actions: {
+      toggleTodo,
+      deleteTodo,
+      editTodo
+    }
+  },
   data () {
   data () {
     return {
     return {
       editing: false
       editing: false
@@ -36,12 +49,6 @@ export default {
     }
     }
   },
   },
   methods: {
   methods: {
-    toggleTodo (todo) {
-      this.$store.actions.toggleTodo(todo)
-    },
-    deleteTodo (todo) {
-      this.$store.actions.deleteTodo(todo)
-    },
     doneEdit (e) {
     doneEdit (e) {
       const value = e.target.value.trim()
       const value = e.target.value.trim()
       if (!value) {
       if (!value) {

+ 9 - 7
examples/todomvc/store/actions.js

@@ -1,8 +1,10 @@
-export default {
-  addTodo: 'ADD_TODO',
-  deleteTodo: 'DELETE_TODO',
-  toggleTodo: 'TOGGLE_TODO',
-  editTodo: 'EDIT_TODO',
-  toggleAll: 'TOGGLE_ALL',
-  clearCompleted: 'CLEAR_COMPLETED'
+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 ({ dispatch }, ...args) => dispatch(type, ...args)
 }
 }

+ 0 - 2
examples/todomvc/store/index.js

@@ -1,6 +1,5 @@
 import Vue from 'vue'
 import Vue from 'vue'
 import Vuex from '../../../src'
 import Vuex from '../../../src'
-import actions from './actions'
 import mutations from './mutations'
 import mutations from './mutations'
 import middlewares from './middlewares'
 import middlewares from './middlewares'
 
 
@@ -13,7 +12,6 @@ const state = {
 
 
 export default new Vuex.Store({
 export default new Vuex.Store({
   state,
   state,
-  actions,
   mutations,
   mutations,
   middlewares
   middlewares
 })
 })