Skip to content

Commit 96084f5

Browse files
ktsnyyx990803
authored andcommitted
support object style dispatch (#315)
1 parent 8cd5a70 commit 96084f5

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ class Store {
8585
}
8686

8787
dispatch (type, payload) {
88+
// check object-style dispatch
89+
if (isObject(type) && type.type) {
90+
payload = type
91+
type = type.type
92+
}
8893
const entry = this._actions[type]
8994
if (!entry) {
9095
console.error(`[vuex] unknown action type: ${type}`)

test/unit/test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ describe('Vuex', () => {
2323
expect(store.state.a).toBe(3)
2424
})
2525

26+
it('committing with object style', () => {
27+
const store = new Vuex.Store({
28+
state: {
29+
a: 1
30+
},
31+
mutations: {
32+
[TEST] (state, payload) {
33+
state.a += payload.amount
34+
}
35+
}
36+
})
37+
store.commit({
38+
type: TEST,
39+
amount: 2
40+
})
41+
expect(store.state.a).toBe(3)
42+
})
43+
2644
it('dispatching actions, sync', () => {
2745
const store = new Vuex.Store({
2846
state: {
@@ -43,6 +61,29 @@ describe('Vuex', () => {
4361
expect(store.state.a).toBe(3)
4462
})
4563

64+
it('dispatching with object style', () => {
65+
const store = new Vuex.Store({
66+
state: {
67+
a: 1
68+
},
69+
mutations: {
70+
[TEST] (state, n) {
71+
state.a += n
72+
}
73+
},
74+
actions: {
75+
[TEST] ({ commit }, payload) {
76+
commit(TEST, payload.amount)
77+
}
78+
}
79+
})
80+
store.dispatch({
81+
type: TEST,
82+
amount: 2
83+
})
84+
expect(store.state.a).toBe(3)
85+
})
86+
4687
it('dispatching actions, with returned Promise', done => {
4788
const store = new Vuex.Store({
4889
state: {

0 commit comments

Comments
 (0)