Przeglądaj źródła

add `createNamespacedHelpers()` to help dealing with namespaced modules (#800)

Fangzhou Li 8 lat temu
rodzic
commit
44d4a72c89
4 zmienionych plików z 73 dodań i 6 usunięć
  1. 7 0
      src/helpers.js
  2. 5 3
      src/index.esm.js
  3. 3 2
      src/index.js
  4. 58 1
      test/unit/helpers.spec.js

+ 7 - 0
src/helpers.js

@@ -70,6 +70,13 @@ export const mapActions = normalizeNamespace((namespace, actions) => {
   return res
 })
 
+export const createNamespacedHelpers = (namespace) => ({
+  mapState: mapState.bind(null, namespace),
+  mapGetters: mapGetters.bind(null, namespace),
+  mapMutations: mapMutations.bind(null, namespace),
+  mapActions: mapActions.bind(null, namespace)
+})
+
 function normalizeMap (map) {
   return Array.isArray(map)
     ? map.map(key => ({ key, val: key }))

+ 5 - 3
src/index.esm.js

@@ -1,5 +1,5 @@
 import { Store, install } from './store'
-import { mapState, mapMutations, mapGetters, mapActions } from './helpers'
+import { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from './helpers'
 
 export default {
   Store,
@@ -8,7 +8,8 @@ export default {
   mapState,
   mapMutations,
   mapGetters,
-  mapActions
+  mapActions,
+  createNamespacedHelpers
 }
 
 export {
@@ -16,5 +17,6 @@ export {
   mapState,
   mapMutations,
   mapGetters,
-  mapActions
+  mapActions,
+  createNamespacedHelpers
 }

+ 3 - 2
src/index.js

@@ -1,5 +1,5 @@
 import { Store, install } from './store'
-import { mapState, mapMutations, mapGetters, mapActions } from './helpers'
+import { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from './helpers'
 
 export default {
   Store,
@@ -8,5 +8,6 @@ export default {
   mapState,
   mapMutations,
   mapGetters,
-  mapActions
+  mapActions,
+  createNamespacedHelpers
 }

+ 58 - 1
test/unit/helpers.spec.js

@@ -1,5 +1,5 @@
 import Vue from 'vue/dist/vue.common.js'
-import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../dist/vuex.common.js'
+import Vuex, { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from '../../dist/vuex.common.js'
 
 describe('Helpers', () => {
   it('mapState (array)', () => {
@@ -321,4 +321,61 @@ describe('Helpers', () => {
     vm.bar()
     expect(b).toHaveBeenCalled()
   })
+
+  it('createNamespacedHelpers', () => {
+    const actionA = jasmine.createSpy()
+    const actionB = jasmine.createSpy()
+    const store = new Vuex.Store({
+      modules: {
+        foo: {
+          namespaced: true,
+          state: { count: 0 },
+          getters: {
+            isEven: state => state.count % 2 === 0
+          },
+          mutations: {
+            inc: state => state.count++,
+            dec: state => state.count--
+          },
+          actions: {
+            actionA,
+            actionB
+          }
+        }
+      }
+    })
+    const {
+      mapState,
+      mapGetters,
+      mapMutations,
+      mapActions
+    } = createNamespacedHelpers('foo/')
+    const vm = new Vue({
+      store,
+      computed: {
+        ...mapState(['count']),
+        ...mapGetters(['isEven'])
+      },
+      methods: {
+        ...mapMutations(['inc', 'dec']),
+        ...mapActions(['actionA', 'actionB'])
+      }
+    })
+    expect(vm.count).toBe(0)
+    expect(vm.isEven).toBe(true)
+    store.state.foo.count++
+    expect(vm.count).toBe(1)
+    expect(vm.isEven).toBe(false)
+    vm.inc()
+    expect(store.state.foo.count).toBe(2)
+    expect(store.getters['foo/isEven']).toBe(true)
+    vm.dec()
+    expect(store.state.foo.count).toBe(1)
+    expect(store.getters['foo/isEven']).toBe(false)
+    vm.actionA()
+    expect(actionA).toHaveBeenCalled()
+    expect(actionB).not.toHaveBeenCalled()
+    vm.actionB()
+    expect(actionB).toHaveBeenCalled()
+  })
 })