Skip to content

Commit 302ed4b

Browse files
committed
update vuex
1 parent 01f67dd commit 302ed4b

File tree

7 files changed

+60
-32
lines changed

7 files changed

+60
-32
lines changed

template/src/components/views/todo/filter/filter.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
-->
77
<template>
88
<div class="filters">
9-
<!-- <button class="cur">All</button>
10-
<button>Active</button>
11-
<button>Completed</button> -->
129
<router-link to="/todo/all" active-class="cur">All</router-link>
1310
<router-link to="/todo/active" active-class="cur">Active</router-link>
1411
<router-link to="/todo/completed" active-class="cur">Completed</router-link>

template/src/components/views/todo/footer/footer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import template from './footer.vue'
1111

1212
import Filters from '../filter'
1313

14-
import { types, module} from 'store/modules/todo'
14+
import { Store } from 'store/modules/todo'
1515

1616
@Component({
1717
name: 'tag-todo-footer',
@@ -21,8 +21,9 @@ import { types, module} from 'store/modules/todo'
2121
}
2222
})
2323
export default class Footer extends Vue {
24-
@module.Getter(types.getter.remaining) remaining: Types.todo.TodoItem[]
25-
@module.Getter(types.getter.completed) completed: Types.todo.TodoItem[]
24+
@Store.getter remaining: Types.todo.TodoItem[]
25+
@Store.getter completed: Types.todo.TodoItem[]
2626

27-
@module.Mutation(types.mutation.clearComplete) clearComplete
27+
@Store.mutation
28+
clearComplete: () => void
2829
}

template/src/components/views/todo/inputBox/inputBox.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Vue from 'components/base'
99
import { Component } from 'vue-property-decorator'
1010
import template from './inputBox.vue'
1111

12-
import { types, module } from 'store/modules/todo'
12+
import { Store } from 'store/modules/todo'
1313

1414
@Component({
1515
name: 'tag-todo-inputbox',
@@ -18,12 +18,15 @@ import { types, module } from 'store/modules/todo'
1818
export default class InputBox extends Vue {
1919
title = ''
2020

21-
@module.State(types.state.todos) todos
21+
@Store.state todos: Types.todo.TodoItem[]
2222

23-
@module.Getter(types.getter.isAllCompleted) isAllCompleted
23+
@Store.getter isAllCompleted: boolean
2424

25-
@module.Mutation(types.mutation.addTodo) addTodo
26-
@module.Mutation(types.mutation.toggleAllTodoStatus) toggleAllStatus
25+
@Store.mutation
26+
addTodo: (todo: Types.todo.TodoItem) => void
27+
28+
@Store.mutation('toggleAllTodoStatus')
29+
toggleAllStatus: (status: boolean) => void
2730

2831
onEnter () {
2932
this.addTodo({

template/src/components/views/todo/item/item.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Vue from 'components/base'
99
import { Component, Prop } from 'vue-property-decorator'
1010
import template from './item.vue'
1111

12-
import { types, module } from 'store/modules/todo'
12+
import { Store } from 'store/modules/todo'
1313

1414
@Component({
1515
name: 'tag-todo-item',
@@ -19,6 +19,12 @@ export default class Item extends Vue {
1919
@Prop()
2020
todo: Types.todo.TodoItem
2121

22+
@Store.mutation('removeTodo')
23+
remove: (todo: Types.todo.TodoItem) => void
24+
25+
@Store.mutation('toggleTodoStatus')
26+
toggleStatus: (payload: { todo: Types.todo.TodoItem, status: boolean }) => void
27+
2228
get status () {
2329
return this.todo.completed
2430
}
@@ -29,7 +35,4 @@ export default class Item extends Vue {
2935
status: value
3036
})
3137
}
32-
33-
@module.Mutation(types.mutation.removeTodo) remove
34-
@module.Mutation(types.mutation.toggleTodoStatus) toggleStatus
3538
}

template/src/components/views/todo/todo.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import TodoItem from './item'
1414
import TodoFooter from './footer'
1515

1616
// vuex
17-
import { types, module } from 'store/modules/todo'
17+
import { Store } from 'store/modules/todo'
1818

1919
@Component({
2020
name: 'view-todo',
@@ -26,25 +26,25 @@ import { types, module } from 'store/modules/todo'
2626
}
2727
})
2828
export default class Todo extends Vue {
29-
@module.State(types.state.todos) allTodos
29+
@Store.state('todos') allTodos: Types.todo.TodoItem[]
3030

31-
@module.Getter(types.getter.filterTodos) todos
31+
@Store.getter('filterTodos') todos: Types.todo.TodoItem[]
3232

33-
@module.Mutation(types.mutation.setFilter) setFilter
34-
@module.Mutation(types.mutation.clearData) clearData
33+
@Store.mutation setFilter: (filter) => void
34+
@Store.mutation clearData: () => void
3535

36-
@module.Action(types.action.fetch) fetch
37-
@module.Action(types.action.save) save
36+
@Store.action fetch: () => Promise<any>
37+
@Store.action save: () => Promise<any>
3838

3939
@Watch('todos', {deep: true})
4040
onTodosChange () {
4141
// save todos
4242
this.save()
4343
}
4444

45-
created () {
45+
async created () {
4646
// fetch todos
47-
this.fetch()
47+
await this.fetch()
4848

4949
if (this.$route.params && this.$route.params.filter) {
5050
this.setFilter(this.$route.params.filter)

template/src/store/modules/todo.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
import Vuex from 'vuex'
66
import { State, Getter, Mutation, Action, namespace } from 'vuex-class'
77
import keymirror from '../utils/keymirror'
8-
import { getter, mutation, action } from '../utils/vuexUtil'
9-
10-
// type alias
11-
type TodoState = Types.State.TodoState
12-
type RootState = Types.State.RootState
8+
import { getter, mutation, action, decorator } from '../utils/vuexUtil'
139

1410
const STORE_KEY = 'vue-typescript-todos'
1511

1612
/*** state ***/
17-
let state: TodoState = {
13+
let state = {
1814
filter: '',
1915
todos: []
2016
}
@@ -103,7 +99,7 @@ let actions = action(state, {
10399
})
104100

105101
/*** module store ***/
106-
let store: Vuex.Module<TodoState, RootState> = {
102+
let store = {
107103
namespaced: true,
108104
state: state,
109105
getters: getters,
@@ -126,4 +122,11 @@ export let module = {
126122
Action: namespace('todo', Action)
127123
}
128124

125+
export let Store = {
126+
state: decorator(module.State, types.state),
127+
getter: decorator(module.Getter, types.getter),
128+
mutation: decorator(module.Mutation, types.mutation),
129+
action: decorator(module.Action, types.action),
130+
}
131+
129132
export default store

template/src/store/utils/vuexUtil.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import Vuex from 'vuex'
6+
import { BindingHelper } from 'vuex-class/lib/bindings'
67

78
// type alias
89
type RootState = Types.State.RootState
@@ -25,3 +26,23 @@ export function mutation<S, T extends MutationTree<S>>(state: S, mutations: T):
2526
export function action<S, T extends ActionTree<S, RootState>>(state: S, actions: T): {[K in keyof T]: Action<S, RootState> } {
2627
return actions
2728
}
29+
30+
export function decorator<D extends BindingHelper, T>(helper: D, keyMap: T) {
31+
type KeyType = keyof T
32+
type Decorator = (target, key: string) => any
33+
34+
function decoratorWrapper(target, key: KeyType): void
35+
function decoratorWrapper(originKey: KeyType): Decorator
36+
function decoratorWrapper(a: any | KeyType, b?: KeyType): Decorator | void {
37+
if (typeof b === 'string') {
38+
const target = a
39+
const key = b
40+
return helper(target, key)
41+
}
42+
43+
const originKey = a
44+
return helper(originKey)
45+
}
46+
47+
return decoratorWrapper
48+
}

0 commit comments

Comments
 (0)