Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 24, 2017
Merged

Conversation

riophae
Copy link
Contributor

@riophae riophae commented May 24, 2017

When using namespaced Vuex module with Vue components, binding namespace to the helper functions (mapState, mapGetters, mapMutations and mapActions) is a bit verbose. For example:

const storeModule = new Vuex.Store({
  namespaced: true,
  state: { val: 0 },
  actions: {
    someAction() {},
  },
})

const store = new Vuex.Store({
  modules: {
    foo: storeModule,
  },
})

new Vue({
  store,
  state: { str: 'abc' },
  computed: {
    // I split up these two state mapping to take advantage of array syntax
    ...mapState('foo', ['val']),
    ...mapState('foo', {
      newVal(state) {
        return state.val + ' ' + this.str
      },
    })
  },
  methods: {
    ...mapActions('foo', ['someAction']),
    someOtherMethod() {},
  },
})

You can see how many times I have repeated to do the binding job to integrate the Vue component with the module assets.

So I wrote a little helper function to help keep the code DRY. You can see the test to learn how that works.

src/helpers.js Outdated
mapNamespacedState: mapState.bind(null, namespace),
mapNamespacedGetters: mapGetters.bind(null, namespace),
mapNamespacedMutations: mapMutations.bind(null, namespace),
mapNamespacedActions: mapActions.bind(null, namespace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use the same name as normal mapXXX helpers here.
It would be more consistent with the normal helpers and the users can rename them if they needs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ktsn Ok, I have followed your advice.

@ktsn ktsn requested a review from yyx990803 May 24, 2017 08:55
@yyx990803 yyx990803 merged commit 44d4a72 into vuejs:dev May 24, 2017
@yyx990803
Copy link
Member

Cool!

@mikeapr4
Copy link

mikeapr4 commented Jun 15, 2017

I went looking in the code to find something to do this exact thing today, I was really pleased when I found it, would be happier if it was released :) in the meantime I'll add it locally as a helper.

Any value in this?:

export const mapForComputation = normalizeNamespace ((namespace, map) => ({
    ...mapState(namespace, {get: map.state}),
    ...map.action && mapAction(namespace, {set: map.action}),
    ...map.mutation && !map.action && mapMutation(namespace, {set: map.mutation})
}))

Usage:

 computed: {
     somevalue: mapForComputation({state: 'somevalue', mutation: 'setSomeValue'})
 }

Again, to reduce verbosity (https://vuex.vuejs.org/en/forms.html). The interface isn't very consistent with the other map* functions, maybe better to name it something else.

Actually maybe the more consistent interface would be:

 computed: {
     ...mapForComputation({
         somevalue: {state: 'somevalue', mutation: 'setSomeValue'}
     })
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants