question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

set value when loading

See original GitHub issue

Custom default value, which will be used until the data is loaded for the first time, but however, how can i set loading value every time with this plugin. Example, how can i set loading status after i click a btn, and before async data is prepared

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
foxbenjaminfoxcommented, Mar 17, 2017

I see what you mean, and I see why you want to use vue-async-computed for that. Indeed, perhaps this is a reasonable use case for vue-async-computed.

Nevertheless, I feel that the example is slightly stretching what vue-async-computed is meant to do: My goal with vue-async-computed is to make the fact that the computed data is asynchronous completely transparent, so that you can treat it almost exactly the same way you’d treat regular computed properties.

But as I said, I do see your use case, and while it is slightly outside the scope of vue-async-computed as I originally envisioned it, I agree it’s a reasonable extension.

There is an easy, but undocumented, way to get vue-async-computed to do this. Hopefully in the future I’ll clean it up and document it properly, if it’s something that people need.

You can overwrite an async computed property synchronously within the asyncComputed function, and the computed value will automatically reset to the correct computed value once the promise resolves.

In other words, you can do this:

const MyComponent = {
  template: `
  <div>
     <button @click="btnClick('something')">Load Something</button>
     <button @click="btnClick2('else')">Load Something else</button>
     <span>{{myData}}</span>
  </div>`,
  data () {
    return {
      loadWhat: 'something',
      loadWhat2: 'else'
    }
  },
  asyncComputed: {
    async myData(){
      this.myData = 'Loading myData'
      return (await this.$http.get('/some/url', { loadWhat: this.loadWhat + this.loadWhat2 })).data
    },
    async myData2(){
      this.myData2 = 'Loading myData2'
      return (await this.$http.get('/some/url2', { loadWhat: this.loadWhat + this.loadWhat2 })).data
    }
  },
  methods: {
    btnClick (loadWhat) {
      this.loadWhat = loadWhat;
    },
    btnClick2 (loadWhat) {
      this.loadWhat2 = loadWhat;
    }
  }
}

This will cause the effect you want: Whenever one of the dependencies change, the value of myData and myData2 immediately (actually, on the next tick) take on a value which is a loading message, while at the same time issuing the async request. Then when the async request resolves, the properties go back to having the appropriate computed value.

0reactions
foxbenjaminfoxcommented, Mar 20, 2017

I’m glad you’ve found your solution.

I will keep this in consideration as a formal feature to add in the future. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set value on page load using javascript?
I have to set dynamically text value when page ...
Read more >
Loading values for single and multiple value attributes
Within each load item, you can include the supportMultipleValuesForADAttributes configuration property and set it to a different value for each load item. For ......
Read more >
how to set default value in data loading function?
I have created a data loading in APEX, and want to record who load the data. so I created one column named user_created....
Read more >
Set default values for fields or controls
This article explains how to set a default value for a table field or for a control on a form in an Access...
Read more >
13.2.9 LOAD DATA Statement
The operation is subject to the secure_file_priv system variable setting: If the variable value is a nonempty directory name, the file must be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found