Skip to content
This repository has been archived by the owner on Aug 8, 2019. It is now read-only.

vvenv/vuex-promise

Repository files navigation

VUEX-PROMISE

💕 A Promise Plugin for Vuex, compatible with 1.0.0-rc.2

Travis Coveralls dependencies devDependency Status NPM version

Usage

set plugin in store

import createPromise from 'vuex-promise'

export default new Vuex.Store({
  strict: __DEV__,
  ...,
  plugins: [createPromise({
    debug: __DEV__,
    status: {
      PENDING: 'PROMISE_PENDING',
      SUCCESS: 'PROMISE_SUCCESS',
      FAILURE: 'PROMISE_FAILURE'
    },
    silent: false
  })]
})

dispatch actions with promisified payloads

import { GET_COMMITS } from '../types'
import request from 'plato-request'

export default {
  getCommits ({ dispatch }, payload) {
    dispatch(GET_COMMITS, request({
      url: '{base}/commits?sha=',
      params: {
        base: 'https://api.github.com/repos/crossjs/plato'
      },
      query: {
        per_page: 3
      },
      headers: {
        'Accept': 'application/vnd.github.v3+json'
      }
    }))
  }
}

handle payloads in module mutations

import { GET_COMMITS } from '../types'
import { PROMISE_SUCCESS } from '../constants'

const state = {
  commits: null
}

const mutations = {
  [GET_COMMITS] (state, { payload, meta }) {
    if (meta === PROMISE_SUCCESS) {
      state.commits = payload
    }
  }
}

export default {
  state,
  mutations
}

License

MIT