From e2ec47e180038b0bb62c93e4152c6392656669f8 Mon Sep 17 00:00:00 2001 From: Johan Hernefeldt Date: Wed, 26 Apr 2017 00:29:25 +0200 Subject: [PATCH] Updated `addStore` and merged `api` into `global` --- README.md | 5 +++ dist/vuex+.js | 61 +++++++++++++++++--------------- package.json | 2 +- src/instanceHandling/addStore.js | 57 +++++++++++++++-------------- src/vuex+.js | 8 ++--- 5 files changed, 70 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index cbaf212..e231a50 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,11 @@ Enhancements: Check out the extensive tutorial bundled with the example: [https://github.com/presidenten/vuex-plus-demo](https://github.com/presidenten/vuex-plus-demo) +### Todo +- Clean up code +- Clean up comments +- Write tests + ### Contributers - [Zyrica](https://github.com/zyrica) diff --git a/dist/vuex+.js b/dist/vuex+.js index 9d6ec10..8f4e5aa 100644 --- a/dist/vuex+.js +++ b/dist/vuex+.js @@ -169,7 +169,7 @@ function setup(newImporter) { * - preserve {boolean}: If true, the store wont be discarded when the final instance is destroyed * @param {string} baseStoreName - The base store name, same as the store filename * @param {Object} loadedModule - The loaded javascript module containing the Vuex module store - * @returns {Object} Vue module mixin + * @returns {storeApi, mixin} api for the loaded module and a mixin */ function add(baseStoreName) { var loadedModule = importer.getModules()[baseStoreName]; @@ -179,43 +179,46 @@ function add(baseStoreName) { } return { - props: ['instance', 'preserve'], - created: function created() { - var this$1 = this; - - baseStoreName = toCamelCase(baseStoreName.replace(/-store$/, '')); - this['$vuex+'] = { - baseStoreName: baseStoreName, - storeInstanceName: getStoreInstanceName(baseStoreName, this.instance), - }; + storeApi: loadedModule.api, + mixin: { + props: ['instance', 'preserve'], + created: function created() { + var this$1 = this; + + baseStoreName = toCamelCase(baseStoreName.replace(/-store$/, '')); + this['$vuex+'] = { + baseStoreName: baseStoreName, + storeInstanceName: getStoreInstanceName(baseStoreName, this.instance), + }; - counter[this['$vuex+'].storeInstanceName] = counter[this['$vuex+'].storeInstanceName] || 0; - counter[this['$vuex+'].storeInstanceName]++; + counter[this['$vuex+'].storeInstanceName] = counter[this['$vuex+'].storeInstanceName] || 0; + counter[this['$vuex+'].storeInstanceName]++; - var getNewInstanceStore = function (newLoadedModule) { return newStore(this$1['$vuex+'].storeInstanceName, this$1.instance, - baseStoreName, newLoadedModule); }; + var getNewInstanceStore = function (newLoadedModule) { return newStore(this$1['$vuex+'].storeInstanceName, this$1.instance, + baseStoreName, newLoadedModule); }; - var store = getNewInstanceStore(loadedModule); - if (!this.$store._modules.root._children[this['$vuex+'].storeInstanceName]) { // eslint-disable-line - this.$store.registerModule(this['$vuex+'].storeInstanceName, store); + var store = getNewInstanceStore(loadedModule); + if (!this.$store._modules.root._children[this['$vuex+'].storeInstanceName]) { // eslint-disable-line + this.$store.registerModule(this['$vuex+'].storeInstanceName, store); - if (module.hot) { - this.$hmrHandler = new HmrHandler(this['$vuex+'].storeInstanceName, getNewInstanceStore); - registerForHMR(this.$hmrHandler, baseStoreName, this['$vuex+'].storeInstanceName); + if (module.hot) { + this.$hmrHandler = new HmrHandler(this['$vuex+'].storeInstanceName, getNewInstanceStore); + registerForHMR(this.$hmrHandler, baseStoreName, this['$vuex+'].storeInstanceName); + } } - } - }, + }, - destroyed: function destroyed() { - counter[this['$vuex+'].storeInstanceName]--; + destroyed: function destroyed() { + counter[this['$vuex+'].storeInstanceName]--; - if (!this.preserve && counter[this['$vuex+'].storeInstanceName] === 0) { - this.$store.unregisterModule(this['$vuex+'].storeInstanceName); + if (!this.preserve && counter[this['$vuex+'].storeInstanceName] === 0) { + this.$store.unregisterModule(this['$vuex+'].storeInstanceName); - if (module.hot) { - unregisterForHMR(this.$hmrHandler); + if (module.hot) { + unregisterForHMR(this.$hmrHandler); + } } - } + }, }, }; } diff --git a/package.json b/package.json index 5cb87fb..d9fe76b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vuex-plus", - "version": "0.4.1", + "version": "0.5.0", "description": "Opinionated library that handles module instances in Vuex", "main": "dist/vuex+.js", "scripts": { diff --git a/src/instanceHandling/addStore.js b/src/instanceHandling/addStore.js index 52039f3..9b7e249 100644 --- a/src/instanceHandling/addStore.js +++ b/src/instanceHandling/addStore.js @@ -15,7 +15,7 @@ export function setup(newImporter) { * - preserve {boolean}: If true, the store wont be discarded when the final instance is destroyed * @param {string} baseStoreName - The base store name, same as the store filename * @param {Object} loadedModule - The loaded javascript module containing the Vuex module store - * @returns {Object} Vue module mixin + * @returns {mixin, api} api for the loaded module and a mixin */ export function add(baseStoreName) { const loadedModule = importer.getModules()[baseStoreName]; @@ -25,41 +25,44 @@ export function add(baseStoreName) { } return { - props: ['instance', 'preserve'], - created() { - baseStoreName = toCamelCase(baseStoreName.replace(/-store$/, '')); - this['$vuex+'] = { - baseStoreName, - storeInstanceName: getStoreInstanceName(baseStoreName, this.instance), - }; + api: loadedModule.api, + mixin: { + props: ['instance', 'preserve'], + created() { + baseStoreName = toCamelCase(baseStoreName.replace(/-store$/, '')); + this['$vuex+'] = { + baseStoreName, + storeInstanceName: getStoreInstanceName(baseStoreName, this.instance), + }; - counter[this['$vuex+'].storeInstanceName] = counter[this['$vuex+'].storeInstanceName] || 0; - counter[this['$vuex+'].storeInstanceName]++; + counter[this['$vuex+'].storeInstanceName] = counter[this['$vuex+'].storeInstanceName] || 0; + counter[this['$vuex+'].storeInstanceName]++; - const getNewInstanceStore = newLoadedModule => newStore(this['$vuex+'].storeInstanceName, this.instance, - baseStoreName, newLoadedModule); + const getNewInstanceStore = newLoadedModule => newStore(this['$vuex+'].storeInstanceName, this.instance, + baseStoreName, newLoadedModule); - const store = getNewInstanceStore(loadedModule); - if (!this.$store._modules.root._children[this['$vuex+'].storeInstanceName]) { // eslint-disable-line - this.$store.registerModule(this['$vuex+'].storeInstanceName, store); + const store = getNewInstanceStore(loadedModule); + if (!this.$store._modules.root._children[this['$vuex+'].storeInstanceName]) { // eslint-disable-line + this.$store.registerModule(this['$vuex+'].storeInstanceName, store); - if (module.hot) { - this.$hmrHandler = new HmrHandler(this['$vuex+'].storeInstanceName, getNewInstanceStore); - registerForHMR(this.$hmrHandler, baseStoreName, this['$vuex+'].storeInstanceName); + if (module.hot) { + this.$hmrHandler = new HmrHandler(this['$vuex+'].storeInstanceName, getNewInstanceStore); + registerForHMR(this.$hmrHandler, baseStoreName, this['$vuex+'].storeInstanceName); + } } - } - }, + }, - destroyed() { - counter[this['$vuex+'].storeInstanceName]--; + destroyed() { + counter[this['$vuex+'].storeInstanceName]--; - if (!this.preserve && counter[this['$vuex+'].storeInstanceName] === 0) { - this.$store.unregisterModule(this['$vuex+'].storeInstanceName); + if (!this.preserve && counter[this['$vuex+'].storeInstanceName] === 0) { + this.$store.unregisterModule(this['$vuex+'].storeInstanceName); - if (module.hot) { - unregisterForHMR(this.$hmrHandler); + if (module.hot) { + unregisterForHMR(this.$hmrHandler); + } } - } + }, }, }; } diff --git a/src/vuex+.js b/src/vuex+.js index eedd338..ca75c7a 100644 --- a/src/vuex+.js +++ b/src/vuex+.js @@ -125,12 +125,6 @@ export const store = storeWrapper; export const hmrCallback = hmrHandler; -/** - * Global api with magical strings to all root modules - * @returns {Object} - Object with magical strings - */ -export const api = apiManager.api; - export const newInstance = function newInstance(substore, instance) { const result = clone(substore); Object.keys(result.api).forEach((type) => { @@ -151,6 +145,8 @@ export const newInstance = function newInstance(substore, instance) { * @returns {any} - Value from Vuex getter */ export const global = { + api: apiManager.api, + get({ path, context }) { const localPath = getLocalPath(path, context);