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 4444</template >
4545
4646<script >
47- import { mapActions } from ' vuex'
47+ import { mapMutations } from ' vuex'
4848import Todo from ' ./Todo.vue'
4949
5050const filters = {
@@ -79,11 +79,11 @@ export default {
7979 addTodo (e ) {
8080 var text = e .target .value
8181 if (text .trim ()) {
82- this .$store .dispatch (' addTodo' , { text })
82+ this .$store .commit (' addTodo' , { text })
8383 }
8484 e .target .value = ' '
8585 },
86- ... mapActions ([
86+ ... mapMutations ([
8787 ' toggleAll' ,
8888 ' clearCompleted'
8989 ])
Original file line number Diff line number Diff line change 1919</template >
2020
2121<script >
22- import { mapActions } from ' vuex'
22+ import { mapMutations } from ' vuex'
2323
2424export default {
2525 name: ' Todo' ,
@@ -39,7 +39,7 @@ export default {
3939 }
4040 },
4141 methods: {
42- ... mapActions ([
42+ ... mapMutations ([
4343 ' editTodo' ,
4444 ' toggleTodo' ,
4545 ' deleteTodo'
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11import Vue from 'vue'
22import Vuex from 'vuex'
3+ import { state , mutations } from './mutations'
34import plugins from './plugins'
4- import * as actions from './actions'
55
66Vue . use ( Vuex )
77
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-
508export default new Vuex . Store ( {
519 state,
52- actions,
5310 mutations,
5411 plugins
5512} )
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 '
22import createLogger from '../../../src/plugins/logger'
33
44const localStoragePlugin = store => {
55 store . subscribe ( ( mutation , { todos } ) => {
6- localStorage . setItem ( STORAGE_KEY , JSON . stringify ( todos ) )
6+ window . localStorage . setItem ( STORAGE_KEY , JSON . stringify ( todos ) )
77 } )
88}
99
You can’t perform that action at this time.
0 commit comments