Coder Social home page Coder Social logo

vue-asyncable's Introduction

vue-asyncable

Async data loading plugin for Vue.js

Coverage Status

Install

npm install vue-asyncable --save-dev

Usage

// assuming CommonJS
import { Asyncable } from 'vue-asyncable'

Then, in your component options, provide an asyncData function:

Vue.component('example', {
  mixins: ['Asyncable'],
  data () {
    return {
      orders: [],
      news: []
    }
  },
  asyncData () {
    return {
      orders: this.$axios.$get('api/orders'),
      news: this.$axios.$get('api/news')
    }
  }
})

Object

Property asyncData can be simple object. In this case you don't need to define initial data elements, the module will set it's automatically

Vue.component('example', {
  // ...
  asyncData: {
    orders: axios.$get('api/orders', { user_id: profile.id }),
    news: axios.$get('api/news', { user_id: profile.id })
  }
})

Promise

You can also return a promise that resolves to the data, and return object with promises and siple types

Vue.component('example', {
  mixins: [Asyncable],
  data () {
    return {
      profile: null,
      orders: null,
      news: null
    }
  }
  async asyncData () {
    let profile = await this.$axios.$get('api/profile')
    return {
      profile,
      orders: this.$axios.$get('api/orders', { user_id: profile.id }),
      news: this.$axios.$get('api/news', { user_id: profile.id })
    }
  }
})

In this case you have to predefine all params in data function

Use promises in data function

You can define props with promises directly in data function and mixin will:

  1. set params to null
  2. call all promise functions
  3. when promises have resolved - assign to data by key
Vue.component('example', {
  mixins: [Asyncable],
  async data () {
    return {
      simpleParam: 'test',
      orders: this.$axios.$post('api/orders', { user_id: profile.id }),
      news: this.$axios.$post('api/news', { user_id: profile.id })
    }
  }
})

Loading State

Your component automatically gets a $loadingAsyncData meta property, which allows you to display a loading state before the data is loaded:

<div v-if="$loadingAsyncData">Loading...</div>
<div v-if="!$loadingAsyncData">Loaded. Put your real content here.</div>

License

MIT

vue-asyncable's People

Contributors

yariksav avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.