Code Monkey home page Code Monkey logo

vue-async-data's Introduction

THIS REPOSITORY IS DEPRECATED

vue-async-data

Async data loading plugin for Vue.js

NOTE:

Install

npm install vue-async-data

Usage

// assuming CommonJS
var Vue = require('vue')
var VueAsyncData = require('vue-async-data')

// use globally
// you can also just use `VueAsyncData.mixin` where needed
Vue.use(VueAsyncData)

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

Vue.component('example', {
  data: function {
    return {
      msg: 'not loaded yet...'
    }
  },
  asyncData: function (resolve, reject) {
    // load data and call resolve(data)
    // or call reject(reason) if something goes wrong
    setTimeout(function () {
      // this will call `vm.$set('msg', 'hi')` for you
      resolve({
        msg: 'hi'
      })
    }, 1000)
  }
})

Promise

You can also return a promise that resolves to the data to be set (plays well with vue-resource):

Vue.component('example', {
  // ...
  asyncData: function () {
    var self = this
    return someServiceThatReturnsPromise.get(12345)
      .then(function (msg) {
        // returning this as the Promise's resolve value
        // will call `vm.$set('msg', msg)` for you
        return {
          msg: msg
        }
        // or, set it yourself:
        // self.msg = msg
      })
  }
})

Parallel fetching with Promise.all and ES6:

Vue.component('example', {
  // ...
  asyncData() {
    return Promise.all([
      serviceA.get(123),
      serviceB.get(234)
    ]).then(([a, b]) => ({a, b}))
  }
})

Reloading Data

The component also gets a method named reloadAsyncData, which obviously reloads the data:

Vue.component('example', {
  // ...
  asyncData() {
    // load data based on `this.params`
  },
  // reload when params change
  watch: {
    params: 'reloadAsyncData'
  }
})

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>

Or, if you prefer to wait until data loaded to display the component, you can use wait-for to listen for the async-data event, which is automatically emitted when the data is loaded:

<example wait-for="async-data"></example>

License

MIT

vue-async-data's People

Contributors

kazupon avatar linusborg avatar miccycn avatar phoenixwong avatar yyx990803 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-async-data's Issues

Using VueAsyncData.mixin throws "[vue-async-data] not installed!" warning.

In vue 1.0.21 there is still mechanism, that throws "[vue-async-data] not installed!" warning
while using vue-async-data as mixin.

<script>
import VueAsyncData from 'vue-async-data';

export default {
  mixins: [
    VueAsyncData.mixin,
  ],
...
  asyncData(resolve, reject) {
    CustomDataService.getItems().then(...

but it works properly!

Bower Support

Please can this package be added to bower. The other official plugins have been added, so it would make sense if this was added as well.

Thanks.

wait-for directive on child elements does not hide contents when operating in vue-router route component

Currently attempting to use vue-async-data and the wait-for directive but it does not hide elements that are inside of a vue-router route component.

<!-- app.html -->
<body>
<router-view></router-view>
<script src="app.js" />
</body>
<!-- component.html -->
<div wait-for="async-data">
This is a child element, should this be visible?
</div>
<div v-if="!$loadingAsyncData">
This is hidden correctly.
</div>
// app.js - snippet
// component and router
var component = Vue.extend({
  template: require("component.html!text"),
  asyncData: function(resolve, reject) {
    setTimeout(function() {
      resolve();
    }, 2000);
  }
});

router.map({
  "/": { component: component }
});

router.start(app, "body");

Edit: after reviewing the code, is it intended for the wait-for directive to only be used inside of a component tag? In that case, it would be impossible to use it in the context of vue-router, as vue-router is responsible for explicitly adding route components to the DOM.

Events not fired from broadcasting

When I try to broadcast event from asyncData I receive no event to child component.

// Parent with async data plugin
asyncData: function () {
    var self = this;
    return this.$http.get('foo.json')
        .then(function (response) {
            self.$broadcast('dataRetrieved', response.data);
            self.data = response.data;
        })
 }

But when I was using broadcast without asyncData I get events to child component

  // Parent without plugin
  methods: {
    getData: function () {

        this.$http.get('foo.json').success(function (data) {
            this.$broadcast('dataRetrieved', data);
            this.data = data;
        }).error(function (err) {
            console.log(err);
        });
    },
 }

Here is my child component, listening for events.

// Child
events: {
    dataRetrieved: function (data) {
       // Here should I get data, but this event is not fired.
        this.data = data;
    }
}

One way binds

Would be awesome if this could 'refresh' one way binds when async data is received

Something like <span> Edit {{* person.name }} </span>

   asyncData: function () {
            var self = this
            return PeopleRedux
                    .single(self.$route.params.personId)
                    .then(function (person) {
                        return {
                            person: person
                        }
                    });
        },

Usage with Vue 2 & vue-router 2

Hi,

looking at this example for data fetching with vue-router v2+ I understand that the async data option has been removed and a pattern for async data very similar to this project is in use.

I'm refactoring a vue 1.x project with migration to v2 in mind, so I was wondering if my assumption is true and whether it'd be an option to adapt this plugin to work with 2.x

If so I'd might also be able to take care of the task and make a PR

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.