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

Allow function for mapMutations/mapActions like mapState #750

Closed
li2568261 opened this issue Apr 20, 2017 · 10 comments
Closed

Allow function for mapMutations/mapActions like mapState #750

li2568261 opened this issue Apr 20, 2017 · 10 comments
Assignees
Labels

Comments

@li2568261
Copy link

What problem does this feature solve?

in function normalizeNamespace

make payload to string is so pain;

can we add the typeOf(val) to judege this type?

my English is poor, please forgive me

What does the proposed API look like?

I want to use mapMutations set payload mutations,like

Vuex.mapMutations({
increame: {
type: 'increame',
addNum: 10
}
})

@ktsn
Copy link
Member

ktsn commented Apr 20, 2017

Do you mean you want to statically set a mutation payload in mapMutation helper? Why isn't it enough for you just to pass the payload to mapped methods?

methods: {
  ...mapMutations({
    increment: 'increment'
  }),

  someMethod () {
    this.increment({ addNum: 10 })
  }
}

@li2568261
Copy link
Author

because the mapState can do like this.
and as u said, do two step.
why not do one step?

@posva
Copy link
Member

posva commented Apr 20, 2017

Because that would make the payload static

@li2568261
Copy link
Author

and a component mutation is more the u defined the function is more。 ithink is nogood

@posva
Copy link
Member

posva commented Apr 20, 2017

Sorry, I don't understand you...
If the amount is always 10, you don't need a payload to set it

@ktsn
Copy link
Member

ktsn commented Apr 20, 2017

@li2568261

mapState can do like this.

I'm afraid I don't understand about this... state has no payload.

do two step.

We always need to call the mapped method even if we can set a payload in helpers. I don't think it has a large difference. I also think we can set the value in actions or mutations if it is really static.

methods: {
  ...mapMutations({
    increment: {
      type: 'increment',
      addNum: 10
    }
  }),

  someMethod () {
    this.increment()
  }
}

@li2568261
Copy link
Author

So so so sry , my english is very poor,if i can‘t express my means,i will be abandon;

// ...
  computed: mapState({
     //...
    // to access local state with `this`, a normal function must be used
    countPlusLocalState (state) {
      return state.count + this.localCount
    }
  })

I means : mutation can be define like countPlusLocalState to pass mutations;

   //eg:
   mapMutations({
      item(mutations){
            var payload = {
                 type:'xxx'
             }
           // the config by componet data; 
            payload.config = this.config;
            mutations(payload)
       }
  })

I think in componet, all about moutation in mapMutations;
I'm read the sourcecode may can do that,pass typeof obj parameter;
this is my reasons for problems;

@ktsn
Copy link
Member

ktsn commented Apr 21, 2017

I see. You need a payload converter between a component and a mutation in mapMutations helper, right?
But I wonder what is the difference of just calling this.$store.commit(payload) in a component method if we have to specify the mutation type in the function.

@li2568261
Copy link
Author

I especially thank you for your patience.
Yes ,I need the converter.
Because .... the this.$store.commit can be omitted.
This is the reasons of the existence of mapMutations;
Do u think so?

@ktsn
Copy link
Member

ktsn commented Apr 25, 2017

Indeed. so the complete api looks like below?

mapMutations(namespace, {
  someMutation (commit, payload) {
    // ...
    commit(type, payload)
  }
})

// ...

vm.someMutation({ value: 123 })

I think mapActions should also have similar API in that case.

mapActions(namespace, {
  someAction (dispatch, payload) {
    // ...
    dispatch(type, payload)
  }
})

@ktsn ktsn changed the title the mutations use Allow function for mapMutations/mapActions like mapState May 9, 2017
@ktsn ktsn self-assigned this Aug 30, 2017
yyx990803 pushed a commit that referenced this issue Aug 31, 2017
#924)

* feat: allow to pass function value in mapActions/mapMutations

* feat: extend mapActions/mapMutations types for function usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants