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

Proposal: Add helpers mapStateTo{Mutation/Action} for improved form handling #953

Closed
darrinmn9 opened this issue Sep 15, 2017 · 2 comments
Closed

Comments

@darrinmn9
Copy link

darrinmn9 commented Sep 15, 2017

What problem does this feature solve?

Provides seamless form handling integration between v-model and Vuex. This allows a user to easily create two-way computed properties for Vuex object properties as outlined at the bottom of https://vuex.vuejs.org/en/forms.html. This removes alot of the verbosity of creating two-way computed properties, and allows an easier transition if someone is refactoring a component written with local Vue data v-model, to Vuex.

(apologies ahead of time if my variable names are poorly chosen)

What does the proposed API look like?

template:

<input v-model="firstName">

<input v-model="lastName">

computed: {
   ...mapState('user', ['profile']),
   ...mapStateToMutation('user', 'updateProfile', this.profile)
}

the specified mutation updateProfile will receive a payload {property, value} anytime the setter is called.

pseudo implementation details:

function mapStateToMutation(namespace, mutationName, vuexObject){
  const twoWayComputed = {};
  const properties = /* all vuexObject own properties */
  
  properties.forEach(function(property) {
    twoWayComputed[property] = {
      get() {
        return vuexObject[property]
      },
      set(value) {
        this.$store.commit(`${nameSpace}/${mutationName}`, {property, value})
      }
    }
  })
  
  return twoWayComputed
}

This similar structure could be used for an action as well, incase a user wanted to perform async operations on update of a property. And going further, the api could be extended to allow custom get/set functions to be defined by the user.

@darrinmn9 darrinmn9 changed the title Proposal: Add helpers mapState{Mutation/Action} for improved form handling Proposal: Add helpers mapStateTo{Mutation/Action} for improved form handling Sep 15, 2017
@darrinmn9
Copy link
Author

is there a reason no one has commented on this?

@nikolay-borzov
Copy link

Similar to #1037

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

No branches or pull requests

2 participants