Skip to content

Commit a057dc0

Browse files
committed
update todomvc example to use new API
1 parent 0109b8b commit a057dc0

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

examples/todomvc/components/App.vue

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
autofocus
1010
autocomplete="off"
1111
placeholder="What needs to be done?"
12-
@keyup.enter="addTodo">
12+
@keyup.enter="tryAddTodo">
1313
</header>
1414
<!-- main section -->
1515
<section class="main" v-show="todos.length">
@@ -47,6 +47,11 @@
4747

4848
<script>
4949
import Todo from './Todo.vue'
50+
import {
51+
addTodo,
52+
toggleAll,
53+
clearCompleted
54+
} from '../store/actions'
5055
5156
const filters = {
5257
all: (todos) => todos,
@@ -56,16 +61,23 @@ const filters = {
5661
5762
export default {
5863
components: { Todo },
64+
vuex: {
65+
state: {
66+
todos: state => state.todos
67+
},
68+
actions: {
69+
addTodo,
70+
toggleAll,
71+
clearCompleted
72+
}
73+
},
5974
data () {
6075
return {
6176
visibility: 'all',
6277
filters: filters
6378
}
6479
},
6580
computed: {
66-
todos () {
67-
return this.$store.state.todos
68-
},
6981
allChecked () {
7082
return this.todos.every(todo => todo.done)
7183
},
@@ -77,18 +89,12 @@ export default {
7789
}
7890
},
7991
methods: {
80-
addTodo (e) {
92+
tryAddTodo (e) {
8193
var text = e.target.value
8294
if (text.trim()) {
83-
this.$store.actions.addTodo(text)
95+
this.addTodo(text)
8496
}
8597
e.target.value = ''
86-
},
87-
toggleAll () {
88-
this.$store.actions.toggleAll()
89-
},
90-
clearCompleted () {
91-
this.$store.actions.clearCompleted()
9298
}
9399
}
94100
}

examples/todomvc/components/Todo.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,21 @@
1919
</template>
2020

2121
<script>
22+
import {
23+
toggleTodo,
24+
deleteTodo,
25+
editTodo
26+
} from '../store/actions'
27+
2228
export default {
2329
props: ['todo'],
30+
vuex: {
31+
actions: {
32+
toggleTodo,
33+
deleteTodo,
34+
editTodo
35+
}
36+
},
2437
data () {
2538
return {
2639
editing: false
@@ -36,12 +49,6 @@ export default {
3649
}
3750
},
3851
methods: {
39-
toggleTodo (todo) {
40-
this.$store.actions.toggleTodo(todo)
41-
},
42-
deleteTodo (todo) {
43-
this.$store.actions.deleteTodo(todo)
44-
},
4552
doneEdit (e) {
4653
const value = e.target.value.trim()
4754
if (!value) {

examples/todomvc/store/actions.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
export default {
2-
addTodo: 'ADD_TODO',
3-
deleteTodo: 'DELETE_TODO',
4-
toggleTodo: 'TOGGLE_TODO',
5-
editTodo: 'EDIT_TODO',
6-
toggleAll: 'TOGGLE_ALL',
7-
clearCompleted: 'CLEAR_COMPLETED'
1+
export const addTodo = makeAction('ADD_TODO')
2+
export const deleteTodo = makeAction('DELETE_TODO')
3+
export const toggleTodo = makeAction('TOGGLE_TODO')
4+
export const editTodo = makeAction('EDIT_TODO')
5+
export const toggleAll = makeAction('TOGGLE_ALL')
6+
export const clearCompleted = makeAction('CLEAR_COMPLETED')
7+
8+
function makeAction (type) {
9+
return ({ dispatch }, ...args) => dispatch(type, ...args)
810
}

examples/todomvc/store/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Vue from 'vue'
22
import Vuex from '../../../src'
3-
import actions from './actions'
43
import mutations from './mutations'
54
import middlewares from './middlewares'
65

@@ -13,7 +12,6 @@ const state = {
1312

1413
export default new Vuex.Store({
1514
state,
16-
actions,
1715
mutations,
1816
middlewares
1917
})

0 commit comments

Comments
 (0)