vuex-xhr-state
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

Vuex xhr state

Use Vuex to manage the state of you're ajax calls.

  • Keep data from ajax calls in your Vuex store
  • Track the state of an individual network load calls in your Vuex store
  • Only fetch data when needed and not already fetching

This package is still in development.

instalation

npm

npm install vuex-xhr-state

yarn

yarn add  vuex-xhr-state

Example

Could be outdated and incomplete feel free to ask questions on gitter (ricky-rick) or e-mail

Store module file logs/index.js

export const xhrLogList = new VuexXhrGet({
  method: (id) => { return api.getAllLogs(id) },
  default: [],
  cache: true,
  store: {
    ...
  },
})
 
export const logs = new VuexXhrCreator('logs', [ xhrLogList ])

Store

import VueXhrState from 'vuex-xhr-state'
import { logs } from './logs/'
 
Vue.use(VueXhrState)
 
const xhrPlugins = [
  logs,
]
 
export default new Vuex.Store({
  plugins: [
    ...xhrPlugins.map((plugin) => plugin.plugin()),
  ],
})

Component

    import { mapActions } from 'vuex'
    import { xhrLogList } from '@/store/logs'
    import { mapXhrGetters } from 'vuex-xhr-state'
    
    export default {
     computed: {
       ...mapXhrGetters({
         logs: xhrLogList.mapData(),
         isLoading: xhrLogList.mapPending(),
       }),
       isLoading: function () {
         return xhrLogList.pending(this.$store.getters)
       },
     },
     methods: {
       ...mapActions({
         fetchLogs: xhrLogList.fetch(),
         forceReload: xhrLogList.forceFetch(),
       }),
     },
     created () {
       this.fetchLogs()
       /**
        * or with custom error handling
        * if the closure returns true the error will not apear in the global VXS store
        */
       const errorHandler(error => {
         return true
       }
       this.fetchLogs({ errorHandler })
     },
    }

Readme

Keywords

Package Sidebar

Install

npm i vuex-xhr-state

Weekly Downloads

31

Version

1.0.5

License

MIT

Unpacked Size

59.7 kB

Total Files

38

Last publish

Collaborators

  • rickyrick