Last active
November 9, 2017 21:52
-
-
Save wtho/c680db5fc9df7bcd17e2a99bd258d85a to your computer and use it in GitHub Desktop.
Vuex mapGetSetters method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function mapGetSetters(object) { | |
| const resolved = {} | |
| const getters = {} | |
| const setters = [] | |
| Object.keys(object).forEach(key => { | |
| resolved[key] = {} | |
| const value = object[key] | |
| if (typeof value === 'string') { | |
| getters[key] = value | |
| } else { | |
| getters[key] = value.get | |
| if ('set' in value) { | |
| setters.push(value.set) | |
| resolved[key].set = 'load' | |
| } | |
| } | |
| }) | |
| const resolvedGetters = mapGetters(getters) | |
| const resolvedSetters = mapMutations(setters) | |
| Object.keys(resolvedGetters).forEach(key => { | |
| resolved[key].get = resolvedGetters[key] | |
| if ('set' in resolved[key]) { | |
| resolved[key].set = resolvedSetters[object[key].set] | |
| } | |
| }) | |
| return resolved | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| computed: { | |
| ...mapGetSetters({ | |
| username: { get: 'USER_GET', set: 'USER_SET' }, | |
| password: { get: 'PASSWORD_GET', set: 'PASSWORD_SET' }, | |
| loading: 'LOADING_GET' | |
| }), | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment