Accept both ways of creating the store

This commit is contained in:
Brendan 2016-12-26 17:19:10 +01:00
parent c2d8329bc0
commit e2d5d2fa61

View File

@ -3,16 +3,36 @@ import Vuex from 'vuex'
Vue.use(Vuex) Vue.use(Vuex)
const files = require.context('~store', false, /^\.\/.*\.js$/) const files = require.context('~store', false, /^\.\/.*\.js$/)
function getModule (filename) {
let file = files(filename)
return file.default
? file.default
: file
}
const storeData = { modules: {} } let store
for (let filename of files.keys()) { let storeData = {}
let name = filename.replace(/^\.\//, '').replace(/\.js$/, '')
if (name === 'index') { // Check if store/index.js returns a vuex store
Object.assign(storeData, files(filename)) if (files.keys().includes('./index.js')) {
let mainModule = getModule('./index.js')
if (mainModule.commit) {
store = mainModule
} else { } else {
storeData.modules[name] = files(filename) storeData = mainModule
storeData.modules[name].namespaced = true
} }
} }
export default new Vuex.Store(storeData) // Generate the store if there is no store yet
if (store == null) {
storeData.modules = storeData.modules || {}
for (let filename of files.keys()) {
let name = filename.replace(/^\.\//, '').replace(/\.js$/, '')
if (name === 'index') continue
storeData.modules[name] = getModule(filename)
storeData.modules[name].namespaced = true
}
store = new Vuex.Store(storeData)
}
export default store