Skip to content

Commit

Permalink
Install as vue & vuex plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
presidenten committed Apr 27, 2017
1 parent 8c25a21 commit eed9bf1
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 31 deletions.
46 changes: 31 additions & 15 deletions dist/vuex+.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 });
});
};

Expand Down Expand Up @@ -233,7 +231,7 @@ function add(baseStoreName) {
}

function setupVuexPlus($store) {
setStore($store);
vuexInstance.store = $store;
var importer = contextHmr.getNewInstance();
setup(importer);
importer.getModules();
Expand Down Expand Up @@ -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));
};

Expand All @@ -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) {
Expand All @@ -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 });
}

Expand All @@ -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 });
}

Expand Down Expand Up @@ -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_;
34 changes: 25 additions & 9 deletions src/api/global.js
Original file line number Diff line number Diff line change
@@ -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));
};

Expand All @@ -19,27 +20,42 @@ 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 });
}

return context.dispatch(path, data, { root: true });
},

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 });
}

Expand Down
7 changes: 2 additions & 5 deletions src/common/hmrHandler.js
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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 });
});
};
5 changes: 3 additions & 2 deletions src/mixins/setupVuexPlus.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/vuex+.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/vuexInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};

0 comments on commit eed9bf1

Please sign in to comment.