Skip to content

Commit

Permalink
Updated addStore and merged api into global
Browse files Browse the repository at this point in the history
  • Loading branch information
presidenten committed Apr 25, 2017
1 parent be24754 commit e2ec47e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 63 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
61 changes: 32 additions & 29 deletions dist/vuex+.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
}
}
}
},
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
57 changes: 30 additions & 27 deletions src/instanceHandling/addStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
}
}
}
},
},
};
}
8 changes: 2 additions & 6 deletions src/vuex+.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);

Expand Down

0 comments on commit e2ec47e

Please sign in to comment.