Skip to content

Commit 9fe44ae

Browse files
committed
tweak todomvc example
1 parent 86c8fde commit 9fe44ae

File tree

6 files changed

+49
-61
lines changed

6 files changed

+49
-61
lines changed

examples/todomvc/components/App.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
</template>
4545

4646
<script>
47-
import { mapActions } from 'vuex'
47+
import { mapMutations } from 'vuex'
4848
import Todo from './Todo.vue'
4949
5050
const 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
])

examples/todomvc/components/Todo.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</template>
2020

2121
<script>
22-
import { mapActions } from 'vuex'
22+
import { mapMutations } from 'vuex'
2323
2424
export default {
2525
name: 'Todo',
@@ -39,7 +39,7 @@ export default {
3939
}
4040
},
4141
methods: {
42-
...mapActions([
42+
...mapMutations([
4343
'editTodo',
4444
'toggleTodo',
4545
'deleteTodo'

examples/todomvc/store/actions.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/todomvc/store/index.js

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,12 @@
11
import Vue from 'vue'
22
import Vuex from 'vuex'
3+
import { state, mutations } from './mutations'
34
import plugins from './plugins'
4-
import * as actions from './actions'
55

66
Vue.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-
508
export default new Vuex.Store({
519
state,
52-
actions,
5310
mutations,
5411
plugins
5512
})

examples/todomvc/store/mutations.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

examples/todomvc/store/plugins.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { STORAGE_KEY } from './index'
1+
import { STORAGE_KEY } from './mutations'
22
import createLogger from '../../../src/plugins/logger'
33

44
const 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

0 commit comments

Comments
 (0)