Create nested modules for each folder in ./store

This commit is contained in:
Ralph Huwiler 2017-03-25 21:21:11 +01:00 committed by GitHub
parent 5ff6424e09
commit 78ea2591c2

View File

@ -2,7 +2,7 @@ import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
let files = require.context('~/store', false, /^\.\/.*\.js$/)
let files = require.context('~/store', true, /^\.\/.*\.js$/)
let filenames = files.keys()
function getModule (filename) {
@ -12,6 +12,18 @@ function getModule (filename) {
: file
}
function getModuleNamespace (storeData, namePath) {
if (namePath.length == 1) return storeData.modules
let namespace = namePath.shift()
storeData.modules[namespace] = storeData.modules[namespace] || {}
storeData.modules[namespace].namespaced = true
storeData.modules[namespace].modules = storeData.modules[namespace].modules || {}
return getModuleNamespace(storeData.modules[namespace], namePath)
}
let store
let storeData = {}
@ -31,8 +43,13 @@ if (store == null) {
for (let filename of filenames) {
let name = filename.replace(/^\.\//, '').replace(/\.js$/, '')
if (name === 'index') continue
storeData.modules[name] = getModule(filename)
storeData.modules[name].namespaced = true
let namePath = name.split(/\//)
let module = getModuleNamespace(storeData, namePath)
name = namePath.pop()
module[name] = getModule(filename)
module[name].namespaced = true
}
store = new Vuex.Store(storeData)
}