return dispatch => { Promise.all([ api.fetchCart(), api.getItems(), api.getHomeSwiper() ]).then(res => { dispatch({ type: 'INIT', user: {}, shoppingCart: res[0], items: res[1], homeSwiper: res[2] }); }); };
return dispatch => { api.getItem(id).then(item => { dispatch({ type: 'SET_ITEM', item }); }); };
import axios from 'axios'; import api from 'api/api.js'; import { hashHistory } from 'react-router'; export default { state: {}, creators: { init({ dispatch }) { Promise.all([ api.fetchCart(), api.getItems(), api.getHomeSwiper() ]).then(res => { dispatch({ type: 'INIT', user: {}, shoppingCart: res[0], items: res[1], homeSwiper: res[2] }); }); } } };
var server = require("api/api.js"); var api = new server(); api.bindWorker("/", function(httpResp){ httpResp.end("Hello, World~"); }); api.startServer(18000, "0.0.0.0");
price: 0 }, action) { switch (action.type) { case 'SET_ITEM': var new_state = JSON.parse(JSON.stringify(state)); for (var key in action.item) { new_state[key] = action.item[key]; } return new_state; default: return state; } }, creators: { fetch_item({ dispatch }, id) { api.getItem(id).then(item => { dispatch({ type: 'SET_ITEM', item }); }); }, addToCart({ dispatch }, item) { new Promise((resolve, reject) => { if (sessionStorage.shoppingCart) { var shoppingCart = JSON.parse(sessionStorage.shoppingCart); shoppingCart.push(item); sessionStorage.shoppingCart = JSON.stringify(shoppingCart); } else { sessionStorage.shoppingCart = '[]'; }