Skip to content

Instantly share code, notes, and snippets.

@samueleiche
Created December 6, 2019 20:35
Show Gist options
  • Save samueleiche/550b0c023f73273b44bb4cbc0e83932b to your computer and use it in GitHub Desktop.
Save samueleiche/550b0c023f73273b44bb4cbc0e83932b to your computer and use it in GitHub Desktop.
Simple state management for Vue.js using Vue observable.
/**
* 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