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

[Feature Request]: Combine @State and @Mutation into a @Computed annotation #40

Open
JavascriptMick opened this issue Dec 10, 2018 · 12 comments

Comments

@JavascriptMick
Copy link

I am trying to bind a checkbox to my Vuex Store. My understanding is that I need to use v-model:-

<b-form-checkbox id="onlyMineFilterCheckbox" v-model="onlyMineFilter">Show only Mine</b-form-checkbox>

To do the binding, I need to create a computed property on my component class. I can either do this...

export default class MyView extends Vue {
  @Mutation updateOnlyMineFilter;
  @State(state => state.onlyMineFilter) onlyMineFilterState: boolean;
  get onlyMineFilter() {
    return this.onlyMineFilterState;
  }
  set onlyMineFilter(value: boolean) {
    this.updateOnlyMineFilter(value);
  }

or, this ..

export default class MyView extends Vue {
  $store: any;
  get onlyMineFilter() {
    return this.$store.state.onlyMineFilter;
  }
  set onlyMineFilter(value: boolean) {
    this.$store.commit('updateOnlyMineFilter', value);
  }

The first option uses the vuex-class annotations which is consistent with the rest of my component, but it is verbose and the state/mutation members are only used once. The second option is less verbose but forces me to use $store directly, with the declaration of $store just to fool typescript which I don't like.

What would be best, I think would be if the state accessor and the mutation could be combined into a single annotation... something like this...

@Computed(state => state.onlyMineFilter, 'updateOnlyMineFilter') onlyMineFilter: boolean;

What do we think? I am happy to attempt the implementation if we think it is a good design.

@chriszrc
Copy link

Could we add a @Setter annotation here so we don't have to use the native get/set?

@JavascriptMick
Copy link
Author

Could we add a @Setter annotation here so we don't have to use the native get/set?

what would that look like? not sure what you are proposing

@JavascriptMick
Copy link
Author

oh, I just saw the mapGetters helper (https://vuex.vuejs.org/guide/getters.html#the-mapgetters-helper) maybe what I actually want is a mapSetters as well?

@jacklp
Copy link

jacklp commented Feb 12, 2019

There is another library that allows for two-way-binding of simple form fields to store variables BUT it does not support typescript:

https://github.com/maoberlehner/vuex-map-fields

There is a real gap in the community for this feature.

@jacklp
Copy link

jacklp commented Feb 12, 2019

Found an easy workaround for now using @change:

<template>
	<v-layout>
		<v-flex>
			<h3>{{ title }}</h3>
			<h4>
				Please enter your work email address and we’ll send you a passcode
			</h4>
			<p>Already registered? <a href="">Sign In</a></p>
			<v-text-field
				v-model="email"
				@change="updateEmail"
				placeholder="Work Email Address"
				outline
			></v-text-field>
			<p>
				To access Apple Employer Portal, enter your email address on file with
				your employer.
			</p>
			<v-btn @click="sendEmail()"></v-btn>
		</v-flex>
	</v-layout>
</template>

<script lang="ts">
import Vue from 'vue'
import { Component, Prop } from 'vue-property-decorator'
import { State, Getter, Mutation, Action, namespace } from 'vuex-class'
import { oneTimePasscode } from '../store/oneTimePasscode'

@Component
export default class OneTimePasscode extends Vue {
	@Prop(String) title!: string
	@Action public sendEmail

	public email: string = ''
	@Mutation public updateEmail
	public created() {
		this.$store.registerModule('oneTimePasscode', oneTimePasscode)
	}
}
</script>

@scleriot
Copy link

@jacklp @JavascriptMick If you're still interested, I just stumbled on the same issue for one of my project, and I just published a tiny library for 2 way binding : https://github.com/scleriot/vuex-class-state2way

@jacklp
Copy link

jacklp commented Feb 12, 2019

sweet, will try and see if I can implement it

@jacklp
Copy link

jacklp commented Feb 12, 2019

worked beautifully @scleriot

@scleriot
Copy link

@jacklp Awesome!

@garyo
Copy link

garyo commented Apr 12, 2019

Is @scleriot 's state2way lib part of vuex-class now?

@gotenxds
Copy link

This should really be part of the library.

@micene09
Copy link

Still waiting for this new feature... what's happening to the project?

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

7 participants