Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active June 9, 2019 09:09
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leodutra/0cfd71fb13e77fa674cc0ceb18477751 to your computer and use it in GitHub Desktop.
Save leodutra/0cfd71fb13e77fa674cc0ceb18477751 to your computer and use it in GitHub Desktop.
Function for mapping Vuex states two way
import { mapState } from 'vuex'
export default function mapStatesTwoWay (namespace, states, updateCb) {
const mappedStates = mapState(namespace, states)
const res = {}
for (const key in mappedStates) {
res[key] = {
set (value) {
updateCb.call(this, { [key]: value })
},
get () {
return mappedStates[key].call(this)
}
}
}
return res
}
@shaunc869
Copy link

This is probably asking way too much but could you show a quick form example, it might be useful for others too where you have a form and are using v-model to do the two way using your idea here? I really think this needs to make it's way into vuex as a standard and this is a great idea, thanks for contributing it!

@leodutra
Copy link
Author

Sorry for the delay... I'll create a module and a demo as soon as possible... please remind me it it takes too long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment