Skip to content

Commit

Permalink
move module specific getters and actions into each module (vuejs#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn authored and yyx990803 committed Nov 10, 2016
1 parent 1fbf9cd commit 8609536
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
17 changes: 0 additions & 17 deletions examples/shopping-cart/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import shop from '../api/shop'
import * as types from './mutation-types'

export const addToCart = ({ commit }, product) => {
Expand All @@ -8,19 +7,3 @@ export const addToCart = ({ commit }, product) => {
})
}
}

export const checkout = ({ commit, state }, products) => {
const savedCartItems = [...state.cart.added]
commit(types.CHECKOUT_REQUEST)
shop.buyProducts(
products,
() => commit(types.CHECKOUT_SUCCESS),
() => commit(types.CHECKOUT_FAILURE, { savedCartItems })
)
}

export const getAllProducts = ({ commit }) => {
shop.getProducts(products => {
commit(types.RECEIVE_PRODUCTS, { products })
})
}
4 changes: 0 additions & 4 deletions examples/shopping-cart/store/getters.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export const checkoutStatus = state => state.cart.checkoutStatus

export const allProducts = state => state.products.all

export const cartProducts = state => {
return state.cart.added.map(({ id, quantity }) => {
const product = state.products.all.find(p => p.id === id)
Expand Down
21 changes: 21 additions & 0 deletions examples/shopping-cart/store/modules/cart.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shop from '../../api/shop'
import * as types from '../mutation-types'

// initial state
Expand All @@ -7,6 +8,24 @@ const state = {
checkoutStatus: null
}

// getters
const getters = {
checkoutStatus: state => state.checkoutStatus
}

// actions
const actions = {
checkout ({ commit, state }, products) {
const savedCartItems = [...state.added]
commit(types.CHECKOUT_REQUEST)
shop.buyProducts(
products,
() => commit(types.CHECKOUT_SUCCESS),
() => commit(types.CHECKOUT_FAILURE, { savedCartItems })
)
}
}

// mutations
const mutations = {
[types.ADD_TO_CART] (state, { id }) {
Expand Down Expand Up @@ -41,5 +60,7 @@ const mutations = {

export default {
state,
getters,
actions,
mutations
}
17 changes: 17 additions & 0 deletions examples/shopping-cart/store/modules/products.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import shop from '../../api/shop'
import * as types from '../mutation-types'

// initial state
const state = {
all: []
}

// getters
const getters = {
allProducts: state => state.all
}

// actions
const actions = {
getAllProducts ({ commit }) {
shop.getProducts(products => {
commit(types.RECEIVE_PRODUCTS, { products })
})
}
}

// mutations
const mutations = {
[types.RECEIVE_PRODUCTS] (state, { products }) {
Expand All @@ -18,5 +33,7 @@ const mutations = {

export default {
state,
getters,
actions,
mutations
}

0 comments on commit 8609536

Please sign in to comment.