Skip to content

Commit

Permalink
Vuex练手
Browse files Browse the repository at this point in the history
  • Loading branch information
丁磊 authored and 丁磊 committed Apr 24, 2016
1 parent 7c1eed9 commit e99c1bf
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 23 deletions.
8 changes: 7 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ module.exports = {
proxyTable: {

}
}
},
API_ROOT:(process.env.NODE_ENV === 'production')
? 'http://www.baidu.com/'
: 'http://localhost:9090/',
CookieDomain : (process.env.NODE_ENV === 'production')
? '.baidu.com'
:''
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"url-loader": "^0.5.7",

"mockjs": "^1.0.0",
"react-cookie": "^0.4.5",


"vue-waterfall":"^0.2.1",
Expand Down
10 changes: 5 additions & 5 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@
import store from './vuex/store'
export default {
data(){
/*data(){
return {
progressbar : false, //loading-bar
effect : 'fade', //路由模板动画参数
routeList : [], //访问周期中所访问了那些路径,在route.js中设置
}
},
store,
components:{
},*/
store
/*components:{
//progressBar
}
}*/
}
</script>
10 changes: 10 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Created by dinglei on 16/4/24.
*/
import { ListResource } from './resources'

export default {
getLists:function () {
return ListResource.get({id: '123'})
},
}
31 changes: 31 additions & 0 deletions src/api/resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Vue from 'vue'
import VueResource from 'vue-resource'
import {API_ROOT} from '../../config'
import { getCookie,signOut } from '../utils/authService'

Vue.use(VueResource)

// HTTP相关
Vue.http.options.crossOrigin = true
Vue.http.options.xhr = {withCredentials: true}

Vue.http.interceptors.push({
request (request) {
// 这里对请求体进行处理
request.headers = request.headers || {}
if (getCookie('token')) {
request.headers.Authorization = 'Bearer ' + getCookie('token').replace(/(^\")|(\"$)/g, '')
}
return request
},
response (response) {
// 这里可以对响应的结果进行处理
if (response.status === 401) {
signOut()
window.location.pathname = '/login'
}
return response
}
})

export const ListResource = Vue.resource(API_ROOT + 'src/mock/list.json')
4 changes: 0 additions & 4 deletions src/api/shop.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/**
* Mocking client-server processing
*/
const _products = [
{"id": 1, "title": "iPad 4 Mini", "price": 500.01, "inventory": 2},
{"id": 2, "title": "H&M T-Shirt White", "price": 10.99, "inventory": 10},
{"id": 3, "title": "Charli XCX - Sucker CD", "price": 19.99, "inventory": 5}
]

export default {
getProducts (cb) {
setTimeout(() => cb(_products), 100)
Expand Down
10 changes: 1 addition & 9 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import configRouter from './routes'
import filters from './utils/filters'

import App from './App.vue'
import lazyload from 'vue-lazyload'
import Mock from 'mockjs'

import './assets/styles/base.css'
//import './assets/styles/base.css'

Vue.config.debug = true

Expand All @@ -21,13 +20,6 @@ Object.keys(filters).forEach(k => Vue.filter(k, filters[k]))

$.ajaxSettings.crossDomain = true;


Vue.use(lazyload, {
error: '../../src/assets/images/common/error.png',
loading: '../../src/assets/images/common/loading.gif',
try: 3 // default 2
});

const router = new VueRouter({
//abstract:true,
//地址栏不会有变化
Expand Down
8 changes: 8 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export default function (router) {
component: require('./App.vue')
},

//数据列表
'/list':{
name:'list',
component: function(reslove){
return require(['./views/list.vue'],reslove)
}
},

//购物车主页
'/cartindex':{
name:'cartindex',
Expand Down
26 changes: 26 additions & 0 deletions src/utils/authService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import cookie from 'react-cookie'
import { CookieDomain } from '../../config.js'
let cookieConfig = {}
if(CookieDomain !== ''){
cookieConfig = { domain: CookieDomain }
}

export function saveCookie(name,value) {
cookie.save(name, value, cookieConfig)
}

export function getCookie(name) {
return cookie.load(name)
}

export function removeCookie(name) {
cookie.remove(name, cookieConfig)
}

export function signOut() {
cookie.remove('token', cookieConfig)
}

export function isLogin() {
return !!cookie.load('token')
}
38 changes: 38 additions & 0 deletions src/views/list.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<style>
</style>

<template>

<ul>
<li v-for="item in lists"> {{item.title}}</li>
</ul>

</template>

<script>
import { getAjax } from '../vuex/actions'
export default {
vuex: {
getters: {
//lists: ({lists}) => lists
lists: function (state) {
console.log(state)
return state.lists
}
},
actions:{
getAjax
}
},
route:{
activate ({ next }) {
this.getAjax()
next()
}
}
}
</script>
18 changes: 15 additions & 3 deletions src/vuex/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import shop from '../api/shop'
import api from '../api'
import * as types from './mutation-types'

export const addToCart = ({ dispatch }, product) => {
Expand All @@ -10,15 +10,27 @@ export const addToCart = ({ dispatch }, product) => {
export const checkout = ({ dispatch, state }, products) => {
const savedCartItems = [...state.cart.added]
dispatch(types.CHECKOUT_REQUEST)
shop.buyProducts(
api.buyProducts(
products,
() => dispatch(types.CHECKOUT_SUCCESS),
() => dispatch(types.CHECKOUT_FAILURE, savedCartItems)
)
}

export const getAllProducts = ({ dispatch }) => {
shop.getProducts(products => {
api.getProducts(products => {
dispatch(types.RECEIVE_PRODUCTS, products)
})
}

export const getAjax = ({ dispatch }) => {
api.getLists().then(response => {
if(!response.code == 0){
return dispatch(types.FAILURE_GET_LISTS)
}
dispatch(types.SUCCESS_GET_LISTS, { lists: response })
}, response => {
dispatch(types.FAILURE_GET_LISTS)
})
}

26 changes: 26 additions & 0 deletions src/vuex/modules/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
SUCCESS_GET_LISTS,
FAILURE_GET_LISTS
} from '../mutation-types'

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

// mutations
const mutations = {
[FAILURE_GET_LISTS](state){
state.items = []
},
[SUCCESS_GET_LISTS](state,action){
// console.log(state)
//console.log(action)
state.items = action.lists.data.data
}
}

export default {
state,
mutations
}
8 changes: 8 additions & 0 deletions src/vuex/mutation-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ export const CHECKOUT_REQUEST = 'CHECKOUT_REQUEST'
export const CHECKOUT_SUCCESS = 'CHECKOUT_SUCCESS'
export const CHECKOUT_FAILURE = 'CHECKOUT_FAILURE'
export const RECEIVE_PRODUCTS = 'RECEIVE_PRODUCTS'





export const SUCCESS_GET_LISTS = 'SUCCESS_GET_LISTS'
export const FAILURE_GET_LISTS = 'FAILURE_GET_LISTS'

4 changes: 3 additions & 1 deletion src/vuex/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Vue from 'vue'
import Vuex from 'vuex'
import cart from './modules/cart'
import products from './modules/products'
import list from './modules/list'
import createLogger from '../../src/middlewares/logger'

Vue.use(Vuex)
Expand All @@ -12,7 +13,8 @@ const debug = process.env.NODE_ENV !== 'production'
export default new Vuex.Store({
modules: {
cart,
products
products,
list
},
strict: debug,
middlewares: debug ? [createLogger()] : []
Expand Down

0 comments on commit e99c1bf

Please sign in to comment.