Skip to content

Instantly share code, notes, and snippets.

@sathiyaseelan
Created July 18, 2018 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sathiyaseelan/c0a46cfdc76e0dff3d57abf81fe4d3d4 to your computer and use it in GitHub Desktop.
Save sathiyaseelan/c0a46cfdc76e0dff3d57abf81fe4d3d4 to your computer and use it in GitHub Desktop.
Nuxt Example for modularised Vuex Store with multiple nuxtServer Init
export const actions = {
async nuxtServerInit ({ dispatch }, { app }) {
console.log('calling init methods')
await dispatch('providers/nuxtServerInit', { app })
await dispatch('products/nuxtServerInit', { app })
}
}
export const state = () => ({
products: []
})
export const getters = {
getProduct (state, id) {
if (state.products) {
return state.products.find( p => p.id == id);
}
return undefined;
},
isEmpty (state){
return !state.products || state.products.length == 0
}
}
export const mutations = {
setProducts(state, products) {
state.products = products
}
}
export const actions = {
async nuxtServerInit ({ commit }, { app }) {
console.log('calling products init method')
let { data } = await app.$axios.$get('https://api.myjson.com/bins/12u91y')
commit('setProducts', data.data)
}
}
export const state = () => ({
providers: []
})
export const getters = {
getProvider (state, id) {
if (state.providers) {
return state.providers.find( p => p.id == id);
}
return undefined;
},
isEmpty (state){
return !state.providers || state.providers.length == 0
}
}
export const mutations = {
setProviders(state, providers) {
state.providers = providers
}
}
export const actions = {
async nuxtServerInit ({ commit }, { app }) {
console.log('calling providers init method')
let { data } = await app.$axios.$get('https://api.myjson.com/bins/xy7v2')
commit('setProviders', data.data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment