Good morning,
I have an API written in Django Rest Framework. The front end is written in VueJS. I have a form view that is either an Add New or a Modify. Basically, the form is the same code, but checks to see if the path includes an ID (and thus, is a modify).
To modify the entry, there are a series of API calls for all the various options in dropdown / select menus. These are one-to-one foreign keys on the model (i.e. an actor has a category) and a couple that are many-to-many keys on the model (i.e. actor has one or more motivations).
My big issues right now is that each of these options (there are fourteen total) has an API call. It seems to me that the options are returning and then rendering without all of API calls finished. In the past, I have just used a boolean value for "loading" to do a v-if statement.
With so many, I had a list of boolean values and embedded within the v-if:
<b-container v-if="load_list.every((x) => x)">
data () {
return {
load_list: [true, true, true, true, true, true, true, true, true],
Then I tried with a computed value:
<b-container v-if="listLoaded">
computed: {
listLoaded: function() {
return this.load_list.every((x) => x)
}
All of the values get adjusted on the returns of the API calls and the list becomes all false, but the container isn’t changing.
Example API call that is called from the Vue created, which calls a function that makes the call to the DRF endpoint with axios (data returns just fine):
created: function() {
API.getActor(this.id)
.then(a => {
this.getActor(a)
})
.catch(error=> console.log(error))
.finally(() => {
this.load_list[0] = false
})
How can I make sure all my of my API return prior to the view rendering? I am sure there is a better way to do it (besides just it not working…), but I am not grasping it in my research.
Thanks for your help.
BCBB
3
Answers
You can use a
watcher
instead of usingcomputed
property it will perfectly fit in you scenario:Watcher official docs
In your case you can watch your
load_list
data property. when whenever any change happen inload_list
then that watcher will run.In the watcher by comparing your values combinations you can set yourlistLoaded
(which you should need to declare within thedata()
)Example watcher code of your scenario
I hope that will help you.
Due to limitations in JavaScript, there are types of changes that Vue cannot detect with arrays and objects. These are discussed in the reactivity section.
See
how about usd componentkey