Created
December 6, 2019 20:35
-
-
Save samueleiche/550b0c023f73273b44bb4cbc0e83932b to your computer and use it in GitHub Desktop.
Simple state management for Vue.js using Vue observable.
This file contains 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
/** | |
* Simple state management for Vue.js. | |
* | |
* @plugin | |
* @example | |
* new Store({ | |
* form: { | |
* state: { | |
* searchText: '', | |
* }, | |
* getters: {}, | |
* actions: {}, | |
* } | |
* }); | |
* | |
* { | |
* computed: { | |
* searchText() { | |
* return this.$store.form.state.searchText, | |
* }, | |
* }, | |
* } | |
*/ | |
class Store { | |
constructor(obj) { | |
this.hydrateStore(obj); | |
Vue.prototype.$store = this; | |
} | |
hydrateStore(obj) { | |
for (const k in obj) { | |
this[k] = { | |
state: Vue.observable(obj[k].state || {}), | |
getters: obj[k].getters || {}, | |
actions: obj[k].actions || {}, | |
}; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment