diff --git a/dist/vuex+.js b/dist/vuex+.js index f339167..0368ece 100644 --- a/dist/vuex+.js +++ b/dist/vuex+.js @@ -85,11 +85,9 @@ var getStoreInstanceName = function (storeName, instance) { var toCamelCase = function (str) { return str.replace(/(-|_)([a-z])/g, function (s) { return s[1].toUpperCase(); }); }; +var vuexInstance = {}; + var handlers = []; -var store$1; -var setStore = function (vuexStore) { - store$1 = vuexStore; -}; var registerForHMR = function (newStore, baseStoreName, storeInstanceName) { handlers.push({ @@ -116,7 +114,7 @@ var hmrHandler = function (updatedModules) { Object.keys(modules).forEach(function (m) { api[m] = remapBaseStore(modules[m].$api, modules[m].name, m); }); - store$1.hotUpdate({ modules: modules }); + vuexInstance.store.hotUpdate({ modules: modules }); }); }; @@ -233,7 +231,7 @@ function add(baseStoreName) { } function setupVuexPlus($store) { - setStore($store); + vuexInstance.store = $store; var importer = contextHmr.getNewInstance(); setup(importer); importer.getModules(); @@ -290,9 +288,9 @@ var _map = { }, }; -var getLocalPath = function (path, context) { - var storeName = context.state['vuex+'].storeName; - var instance = context.state['vuex+'].instance; +var getLocalPath = function (path, state) { + var storeName = state['vuex+'].storeName; + var instance = state['vuex+'].instance; return path.replace(storeName, getStoreInstanceName(storeName, instance)); }; @@ -310,14 +308,24 @@ var _global = { get: function get(ref) { var path = ref.path; var context = ref.context; + var state = ref.state; var local = ref.local; + if (!state && !context) { + console.error('Cant global.get without `store` or `context`'); + } if (local) { - var localPath = getLocalPath(path, context); - return context.rootGetters[localPath]; + var localPath = getLocalPath(path, state || context.state); + if (context) { + return context.rootGetters[localPath]; + } + return vuexInstance.store.getters[localPath]; } - return context.rootGetters[path]; + if (context) { + return context.rootGetters[path]; + } + return vuexInstance.store.getters[path]; }, dispatch: function dispatch(ref) { @@ -326,8 +334,11 @@ var _global = { var context = ref.context; var local = ref.local; + if (!context) { + console.error('Cant global.dispatch without `context`'); + } if (local) { - var localPath = getLocalPath(path, context); + var localPath = getLocalPath(path, context.state); return context.dispatch(localPath, data, { root: true }); } @@ -340,8 +351,11 @@ var _global = { var context = ref.context; var local = ref.local; + if (!context) { + console.error('Cant global.commit without `context`'); + } if (local) { - var localPath = getLocalPath(path, context); + var localPath = getLocalPath(path, context.state); return context.commit(localPath, data, { root: true }); } @@ -466,9 +480,11 @@ var addStore = add; var hmrCallback = hmrHandler; var newInstance = _newInstance; +var $store = vuexInstance.store; + var vuex_ = { vuePlugin: _vuePluginInstall, vuexPlugin: setupVuexPlus, }; -export { map, store, global, addStore, hmrCallback, newInstance };export default vuex_; +export { map, store, global, addStore, hmrCallback, newInstance, $store };export default vuex_; diff --git a/src/api/global.js b/src/api/global.js index b2d74b8..a2bb813 100644 --- a/src/api/global.js +++ b/src/api/global.js @@ -1,10 +1,11 @@ import clone from 'clone'; import { api } from './api.js'; import { getStoreInstanceName } from '../common/helpers.js'; +import vuexInstance from '../vuexInstance.js'; -const getLocalPath = (path, context) => { - const storeName = context.state['vuex+'].storeName; - const instance = context.state['vuex+'].instance; +const getLocalPath = (path, state) => { + const storeName = state['vuex+'].storeName; + const instance = state['vuex+'].instance; return path.replace(storeName, getStoreInstanceName(storeName, instance)); }; @@ -19,18 +20,30 @@ export default { return clone(api); }, - get({ path, context, local }) { + get({ path, context, state, local }) { + if (!state && !context) { + console.error('Cant global.get without `store` or `context`'); + } if (local) { - const localPath = getLocalPath(path, context); - return context.rootGetters[localPath]; + const localPath = getLocalPath(path, state || context.state); + if (context) { + return context.rootGetters[localPath]; + } + return vuexInstance.store.getters[localPath]; } - return context.rootGetters[path]; + if (context) { + return context.rootGetters[path]; + } + return vuexInstance.store.getters[path]; }, dispatch({ path, data, context, local }) { + if (!context) { + console.error('Cant global.dispatch without `context`'); + } if (local) { - const localPath = getLocalPath(path, context); + const localPath = getLocalPath(path, context.state); return context.dispatch(localPath, data, { root: true }); } @@ -38,8 +51,11 @@ export default { }, commit({ path, data, context, local }) { + if (!context) { + console.error('Cant global.commit without `context`'); + } if (local) { - const localPath = getLocalPath(path, context); + const localPath = getLocalPath(path, context.state); return context.commit(localPath, data, { root: true }); } diff --git a/src/common/hmrHandler.js b/src/common/hmrHandler.js index 004f94a..52d13b2 100644 --- a/src/common/hmrHandler.js +++ b/src/common/hmrHandler.js @@ -1,11 +1,8 @@ import { api, remapBaseStore } from '../api/api.js'; import { toCamelCase } from './helpers.js'; +import vuexInstance from '../vuexInstance.js'; let handlers = []; -let store; -export const setStore = (vuexStore) => { - store = vuexStore; -}; export const registerForHMR = (newStore, baseStoreName, storeInstanceName) => { handlers.push({ @@ -32,6 +29,6 @@ export const hmrHandler = (updatedModules) => { Object.keys(modules).forEach((m) => { api[m] = remapBaseStore(modules[m].$api, modules[m].name, m); }); - store.hotUpdate({ modules }); + vuexInstance.store.hotUpdate({ modules }); }); }; diff --git a/src/mixins/setupVuexPlus.js b/src/mixins/setupVuexPlus.js index f87ecda..241f892 100644 --- a/src/mixins/setupVuexPlus.js +++ b/src/mixins/setupVuexPlus.js @@ -1,9 +1,10 @@ import contextHmr from 'webpack-context-vuex-hmr'; import { setup } from './addStore.js'; -import { hmrHandler, setStore } from '../common/hmrHandler.js'; +import { hmrHandler } from '../common/hmrHandler.js'; +import vuexInstance from '../vuexInstance.js'; export default function setupVuexPlus($store) { - setStore($store); + vuexInstance.store = $store; const importer = contextHmr.getNewInstance(); setup(importer); importer.getModules(); diff --git a/src/vuex+.js b/src/vuex+.js index bb646c7..758e52a 100644 --- a/src/vuex+.js +++ b/src/vuex+.js @@ -7,6 +7,7 @@ import _store from './store/storeWrapper.js'; import _addStore from './mixins/addStore.js'; import _newInstance from './store/newInstance'; import _vuePluginInstall from './mixins/install.js'; +import _vuexInstance from './vuexInstance.js'; export const map = _map; export const store = _store; @@ -15,6 +16,8 @@ export const addStore = _addStore; export const hmrCallback = hmrHandler; export const newInstance = _newInstance; +export const $store = _vuexInstance.store; + export default { vuePlugin: _vuePluginInstall, vuexPlugin: setupVuexPlus, diff --git a/src/vuexInstance.js b/src/vuexInstance.js new file mode 100644 index 0000000..ff8b4c5 --- /dev/null +++ b/src/vuexInstance.js @@ -0,0 +1 @@ +export default {};