Before I started using the component for the layout, the map function would re-render the posts list whenever I called $update();. But after switching to a component, the posts list does not re-render when I call $update();. New items are not displayed, and deleted items do not disappear.
@deejay I kept my original structure and added an additional $update() call at the beginning of my getLatestPosts() function. That seems to have solved the problem.
It’s strange, because I was already calling $update() at the end of getLatestPosts(), so I’m not sure why calling twice would make a difference. But it works!
@deejay I think I know what’s happening. When I add a new post, I trigger a refreshPosts() function which empties the post array, re-initializes some infinite scroll variables and then calls getLatestPosts(). I didn’t have the $update() in the refreshPosts function since it’s called at the end of getLatestPosts()…but, it seems that when using a component, the update at the end of getLatestPosts wasn’t causing the list to refresh…my guess is because the index values were the same. So, I added $update() to refreshPosts(), just after I empty the posts array.
Here’s my setup
let posts = [];
let lastPostTimestamp;
let allowInfinite = true;
let hasMorePosts = true;
const refreshPosts = ( e, done ) => { //also called by pull-to-refresh
posts = [];
lastPostTimestamp = null;
allowInfinite = true;
hasMorePosts = true;
$update(); // necessary for the list to refresh when using local component for layout
getLatestPosts();
if (done) {
done();
}
}
const getLatestPosts = ( ) => {
if (!allowInfinite) return;
allowInfinite = false;
... // query for latest posts
var counter = 0
queryResults.forEach( async (result) => {
... // process the results (including some async queries to get user data)
posts.push(post);
counter ++;
if (counter == queryResults.length ) {
$update();
}
});
if ( queryResults.length < queryLimit ) {
hasMorePosts = false;
$update();
return;
}
allowInfinite = true;
$update();
}
$onMounted(() => {
getLatestPosts();
});
$on('pageInit', (e, page) => {
$f7.on('postCreated', function () {
refreshPosts();
});
$f7.on('postDeleted', function () {
refreshPosts();
});
})