I need to be able to switch between lists to display in vuejs. So something like:
<app-card
v-for="card in cardsCache[currentCategory]"
:key="card"
:affiliateLink="card.affiliateLink"
:author="card.author"
:category="card.category"
:comments="card.comments"
:description="card.description"
:downvotes="card.downvotes"
:id="card.id"
:imageUrl="card.imageUrl"
:timeCreated="card.timeCreated"
:title="card.title"
:upvotes="card.upvotes"
@click-card="clickCard"
@click-upvote="clickUpvote"
@click-downvote="clickDownvote"
@click-affiliateLink="clickAffiliateLink"
@click-comments="clickComments"
@click-save="clickSave"
></app-card>
Notice what I'm doing with the vfor, I'm accessing an object with the currentCategory variable which could change at anytime. This sort of works but has issues. When I initially switch category it will render as expected, but if I continue to add to the array for the recently switched to category it will not continue to render to render what's being added to the list. And yes I checked the list is actually having items be added. I've found that any lists added to the cardsCache object as a literal, so: cardsCache: {"popular": []} then this issue does not happen for that list. I've also found that if I switch to and from the same list it will suddenly render all the new items added to it. It's like vuejs has trouble watching objects. I would think what I'm trying to do is fairly basic but I haven't been able to find any examples of people doing this.
Below is how I'm adding data to be displayed:
const self = this;
if (Util.nullOrUndefined(self.cardsCache[category])) {
self.cardsCache[category] = [];
}
if (self.previousCategory != self.currentCategory) {
Util.scrollToTop();
}
if (this.loadingCards === false) {
if (category == "search") {
this.cardSearch();
} else {
this.loadingCards = true;
ApiCalls.loadCardsFromCategory(
this,
category,
resp => {
let cards = resp.data.resp.api_data.cards;
parseCards(cards).forEach(c => {
self.cardsCache[category].push(c);
});
self.previousCategory = self.currentCategory;
self.currentCategory = category;
self.loadingCards = false;
},
(error, context) => {
console.log(error);
self.loadingCards = false;
},
this.userId
);
}
}
},```
You'll want to pay attention to the line where I say ```self.cardsCache[category].push(c)```
isprop.