// index.ts import Vue from 'vue'; import Vuex, { StoreOptions } from 'vuex'; import RootState from './types/RootStateModel'; import { alertStore as alerts } from './modules/alert-store'; import { assetStore as assets } from './modules/asset-store'; import { dataStore as data } from './modules/data-store'; import { permissionStore as permissions } from './modules/permission-store'; import { pageStore as pages } from './modules/page-store'; import { languageStore as languages } from './modules/language-store'; import { VueRouter } from 'vue-router/types/router'; Vue.use(Vuex); export const storeConfig: StoreOptions = { modules: { alerts, assets, data, permissions, pages, languages } }; const LANGUAGE_INIT_ACTION_NAME = 'languages/$init'; export class CmsStore extends Vuex.Store { public async initialize(router: VueRouter) { const languageAction = this.actions[LANGUAGE_INIT_ACTION_NAME]; try { await this.dispatch('data/fetchCurrentUser'); // The language store should be initialized first, as other store initial calls depend on the available languages if (languageAction && languageAction[0]) { await languageAction[0](); } else { console.error('[CMS-Core] Language store has no $init action, make sure it exists.'); } // Iterate over all actions inside or outside of modules, in search of '$init' actions to execute. for (const actionName of Object.keys(this.actions)) { // The language store has already been initialized prior to all other stores if (actionName === LANGUAGE_INIT_ACTION_NAME) { continue; } if (!actionName.match(/\$init(?:$|\W)/i)) { continue; } // Execute an $init action is if it was called by Vuex. this.actions[actionName][0](router); } } catch (e) { console.error(e); } } private get actions() { // @ts-ignore // Get the internal Vuex actions variable, containing all module actions. return this._actions as { [actionName: string]: Function[] }; } } export default new CmsStore(storeConfig);