ソースを参照

fix object-style dispatch

Evan You 9 年 前
コミット
db8b44f81f
2 ファイル変更32 行追加14 行削除
  1. 25 7
      src/index.js
  2. 7 7
      test/unit/test.js

+ 25 - 7
src/index.js

@@ -83,9 +83,11 @@ class Store {
 
   dispatch (type, ...payload) {
     let silent = false
+    let isObjectStyleDispatch = false
     // compatibility for object actions, e.g. FSA
     if (typeof type === 'object' && type.type && arguments.length === 1) {
-      payload = [type.payload]
+      isObjectStyleDispatch = true
+      payload = type
       if (type.silent) silent = true
       type = type.type
     }
@@ -95,12 +97,20 @@ class Store {
       this._dispatching = true
       // apply the mutation
       if (Array.isArray(mutation)) {
-        mutation.forEach(m => m(state, ...payload))
+        mutation.forEach(m => {
+          isObjectStyleDispatch
+            ? m(state, payload)
+            : m(state, ...payload)
+        })
       } else {
-        mutation(state, ...payload)
+        isObjectStyleDispatch
+          ? mutation(state, payload)
+          : mutation(state, ...payload)
       }
       this._dispatching = false
-      if (!silent) this._applyMiddlewares(type, payload)
+      if (!silent) {
+        this._applyMiddlewares(type, payload, isObjectStyleDispatch)
+      }
     } else {
       console.warn(`[vuex] Unknown mutation: ${type}`)
     }
@@ -273,9 +283,10 @@ class Store {
    *
    * @param {String} type
    * @param {Array} payload
+   * @param {Boolean} isObjectStyleDispatch
    */
 
-  _applyMiddlewares (type, payload) {
+  _applyMiddlewares (type, payload, isObjectStyleDispatch) {
     const state = this.state
     const prevSnapshot = this._prevSnapshot
     let snapshot, clonedPayload
@@ -285,10 +296,17 @@ class Store {
     }
     this._middlewares.forEach(m => {
       if (m.onMutation) {
+        const mutation = isObjectStyleDispatch
+          ? m.snapshot
+            ? clonedPayload
+            : payload
+          : m.snapshot
+            ? { type, payload: clonedPayload }
+            : { type, payload }
         if (m.snapshot) {
-          m.onMutation({ type, payload: clonedPayload }, snapshot, prevSnapshot, this)
+          m.onMutation(mutation, snapshot, prevSnapshot, this)
         } else {
-          m.onMutation({ type, payload }, state, this)
+          m.onMutation(mutation, state, this)
         }
       }
     })

+ 7 - 7
test/unit/test.js

@@ -348,8 +348,8 @@ describe('Vuex', () => {
         a: 1
       },
       mutations: {
-        [TEST] (state, n) {
-          state.a += n
+        [TEST] (state, { payload }) {
+          state.a += payload
         }
       },
       middlewares: [
@@ -378,8 +378,8 @@ describe('Vuex', () => {
     expect(mutations.length).to.equal(2)
     expect(mutations[0].type).to.equal(TEST)
     expect(mutations[1].type).to.equal(TEST)
-    expect(mutations[0].payload[0]).to.equal(1)
-    expect(mutations[1].payload[0]).to.equal(2)
+    expect(mutations[0].payload[0]).to.equal(1) // normal dispatch
+    expect(mutations[1].payload).to.equal(2) // object dispatch
   })
 
   it('watch', function (done) {
@@ -525,14 +525,14 @@ describe('Vuex', () => {
         a: 1
       },
       mutations: {
-        [TEST] (state, amount) {
-          state.a += amount
+        [TEST] (state, { by }) {
+          state.a += by
         }
       }
     })
     store.dispatch({
       type: TEST,
-      payload: 2
+      by: 2
     })
     expect(store.state.a).to.equal(3)
   })