File tree Expand file tree Collapse file tree 6 files changed +49
-61
lines changed Expand file tree Collapse file tree 6 files changed +49
-61
lines changed Original file line number Diff line number Diff line change 44
44
</template >
45
45
46
46
<script >
47
- import { mapActions } from ' vuex'
47
+ import { mapMutations } from ' vuex'
48
48
import Todo from ' ./Todo.vue'
49
49
50
50
const filters = {
@@ -79,11 +79,11 @@ export default {
79
79
addTodo (e ) {
80
80
var text = e .target .value
81
81
if (text .trim ()) {
82
- this .$store .dispatch (' addTodo' , { text })
82
+ this .$store .commit (' addTodo' , { text })
83
83
}
84
84
e .target .value = ' '
85
85
},
86
- ... mapActions ([
86
+ ... mapMutations ([
87
87
' toggleAll' ,
88
88
' clearCompleted'
89
89
])
Original file line number Diff line number Diff line change 19
19
</template >
20
20
21
21
<script >
22
- import { mapActions } from ' vuex'
22
+ import { mapMutations } from ' vuex'
23
23
24
24
export default {
25
25
name: ' Todo' ,
@@ -39,7 +39,7 @@ export default {
39
39
}
40
40
},
41
41
methods: {
42
- ... mapActions ([
42
+ ... mapMutations ([
43
43
' editTodo' ,
44
44
' toggleTodo' ,
45
45
' deleteTodo'
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
import Vue from 'vue'
2
2
import Vuex from 'vuex'
3
+ import { state , mutations } from './mutations'
3
4
import plugins from './plugins'
4
- import * as actions from './actions'
5
5
6
6
Vue . use ( Vuex )
7
7
8
- export const STORAGE_KEY = 'todos-vuejs'
9
-
10
- // for testing
11
- if ( navigator . userAgent . indexOf ( 'PhantomJS' ) > - 1 ) {
12
- localStorage . clear ( )
13
- }
14
-
15
- const state = {
16
- todos : JSON . parse ( localStorage . getItem ( STORAGE_KEY ) || '[]' )
17
- }
18
-
19
- const mutations = {
20
- ADD_TODO ( state , { text } ) {
21
- state . todos . push ( {
22
- text,
23
- done : false
24
- } )
25
- } ,
26
-
27
- DELETE_TODO ( state , { todo } ) {
28
- state . todos . splice ( state . todos . indexOf ( todo ) , 1 )
29
- } ,
30
-
31
- TOGGLE_TODO ( state , { todo } ) {
32
- todo . done = ! todo . done
33
- } ,
34
-
35
- EDIT_TODO ( state , { todo, value } ) {
36
- todo . text = value
37
- } ,
38
-
39
- TOGGLE_ALL ( state , { done } ) {
40
- state . todos . forEach ( ( todo ) => {
41
- todo . done = done
42
- } )
43
- } ,
44
-
45
- CLEAR_COMPLETED ( state ) {
46
- state . todos = state . todos . filter ( todo => ! todo . done )
47
- }
48
- }
49
-
50
8
export default new Vuex . Store ( {
51
9
state,
52
- actions,
53
10
mutations,
54
11
plugins
55
12
} )
Original file line number Diff line number Diff line change
1
+ export const STORAGE_KEY = 'todos-vuejs'
2
+
3
+ // for testing
4
+ if ( navigator . userAgent . indexOf ( 'PhantomJS' ) > - 1 ) {
5
+ window . localStorage . clear ( )
6
+ }
7
+
8
+ export const state = {
9
+ todos : JSON . parse ( window . localStorage . getItem ( STORAGE_KEY ) || '[]' )
10
+ }
11
+
12
+ export const mutations = {
13
+ addTodo ( state , { text } ) {
14
+ state . todos . push ( {
15
+ text,
16
+ done : false
17
+ } )
18
+ } ,
19
+
20
+ deleteTodo ( state , { todo } ) {
21
+ state . todos . splice ( state . todos . indexOf ( todo ) , 1 )
22
+ } ,
23
+
24
+ toggleTodo ( state , { todo } ) {
25
+ todo . done = ! todo . done
26
+ } ,
27
+
28
+ editTodo ( state , { todo, value } ) {
29
+ todo . text = value
30
+ } ,
31
+
32
+ toggleAll ( state , { done } ) {
33
+ state . todos . forEach ( ( todo ) => {
34
+ todo . done = done
35
+ } )
36
+ } ,
37
+
38
+ clearCompleted ( state ) {
39
+ state . todos = state . todos . filter ( todo => ! todo . done )
40
+ }
41
+ }
Original file line number Diff line number Diff line change 1
- import { STORAGE_KEY } from './index '
1
+ import { STORAGE_KEY } from './mutations '
2
2
import createLogger from '../../../src/plugins/logger'
3
3
4
4
const localStoragePlugin = store => {
5
5
store . subscribe ( ( mutation , { todos } ) => {
6
- localStorage . setItem ( STORAGE_KEY , JSON . stringify ( todos ) )
6
+ window . localStorage . setItem ( STORAGE_KEY , JSON . stringify ( todos ) )
7
7
} )
8
8
}
9
9
You can’t perform that action at this time.
0 commit comments