File tree Expand file tree Collapse file tree 4 files changed +40
-27
lines changed Expand file tree Collapse file tree 4 files changed +40
-27
lines changed Original file line number Diff line number Diff line change 9
9
autofocus
10
10
autocomplete =" off"
11
11
placeholder =" What needs to be done?"
12
- @keyup.enter =" addTodo " >
12
+ @keyup.enter =" tryAddTodo " >
13
13
</header >
14
14
<!-- main section -->
15
15
<section class =" main" v-show =" todos.length" >
47
47
48
48
<script >
49
49
import Todo from ' ./Todo.vue'
50
+ import {
51
+ addTodo ,
52
+ toggleAll ,
53
+ clearCompleted
54
+ } from ' ../store/actions'
50
55
51
56
const filters = {
52
57
all : (todos ) => todos,
@@ -56,16 +61,23 @@ const filters = {
56
61
57
62
export default {
58
63
components: { Todo },
64
+ vuex: {
65
+ state: {
66
+ todos : state => state .todos
67
+ },
68
+ actions: {
69
+ addTodo,
70
+ toggleAll,
71
+ clearCompleted
72
+ }
73
+ },
59
74
data () {
60
75
return {
61
76
visibility: ' all' ,
62
77
filters: filters
63
78
}
64
79
},
65
80
computed: {
66
- todos () {
67
- return this .$store .state .todos
68
- },
69
81
allChecked () {
70
82
return this .todos .every (todo => todo .done )
71
83
},
@@ -77,18 +89,12 @@ export default {
77
89
}
78
90
},
79
91
methods: {
80
- addTodo (e ) {
92
+ tryAddTodo (e ) {
81
93
var text = e .target .value
82
94
if (text .trim ()) {
83
- this .$store . actions . addTodo (text)
95
+ this .addTodo (text)
84
96
}
85
97
e .target .value = ' '
86
- },
87
- toggleAll () {
88
- this .$store .actions .toggleAll ()
89
- },
90
- clearCompleted () {
91
- this .$store .actions .clearCompleted ()
92
98
}
93
99
}
94
100
}
Original file line number Diff line number Diff line change 19
19
</template >
20
20
21
21
<script >
22
+ import {
23
+ toggleTodo ,
24
+ deleteTodo ,
25
+ editTodo
26
+ } from ' ../store/actions'
27
+
22
28
export default {
23
29
props: [' todo' ],
30
+ vuex: {
31
+ actions: {
32
+ toggleTodo,
33
+ deleteTodo,
34
+ editTodo
35
+ }
36
+ },
24
37
data () {
25
38
return {
26
39
editing: false
@@ -36,12 +49,6 @@ export default {
36
49
}
37
50
},
38
51
methods: {
39
- toggleTodo (todo ) {
40
- this .$store .actions .toggleTodo (todo)
41
- },
42
- deleteTodo (todo ) {
43
- this .$store .actions .deleteTodo (todo)
44
- },
45
52
doneEdit (e ) {
46
53
const value = e .target .value .trim ()
47
54
if (! value) {
Original file line number Diff line number Diff line change 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 )
8
10
}
Original file line number Diff line number Diff line change 1
1
import Vue from 'vue'
2
2
import Vuex from '../../../src'
3
- import actions from './actions'
4
3
import mutations from './mutations'
5
4
import middlewares from './middlewares'
6
5
@@ -13,7 +12,6 @@ const state = {
13
12
14
13
export default new Vuex . Store ( {
15
14
state,
16
- actions,
17
15
mutations,
18
16
middlewares
19
17
} )
You can’t perform that action at this time.
0 commit comments