I am using rss2json to consume an rss feed. There is not a page param to enable pagination. There is a count parameter that I can pass to the request. I am able to load the feed and get results back. I have created a service using ionic to make a request to get the feed:
getRssFeed(rssUrl: string, count: number) {
return new Promise<any>(resolve => {
this.http.get(`${ environment.podcast.baseUrl }?rss_url=${ rssUrl }&api_key=${ environment.podcast.apiKey }&count=${ count }`)
.subscribe(data => {
resolve(data);
}, error => {
console.error('Something really bad happened trying to get rss feed.');
console.error(error);
});
});
}
This works great. I can get the data back - all is well. I am using an infinite scroll component to handle pagination. Again, all is well. I am starting with 10 podcast episodes. I am logging out when I want to load more episodes:
When I scroll, the service makes the correct call, but because the rss2json service does not have a page param, It will return the entire array when I update the count.
So I need to do something like this:
episodes: Array<any>;
count = 10;
...
this.episodes.splice(this.episodes.length, this.count, data.items);
I need to find out how many episodes I already have. The first time I get to the bottom of my list, I'll have 10 (I want to increment +10 each load). So I need to:
- Find out how many episodes I currently have (10, 20, 30 etc.)
- Make the request to get more episodes
- Service returns 20 episodes -- but it will always start at zero.
- Slice the first 10, 20, ?? episodes that are returned, add the remaining 10 to the end of the list.
I am not sure how to achieve this and could use some direction.
Here is how I am requesting more episodes:
this.myPodcastService.getRssFeed(this.rssUrl, this.count)
.then(data => {
if (data) {
// console.log('data', data.items);
// data is an object
// data.items is an array of episodes
// this.episodes.splice(this.episodes.length, this.count, data.items);
} else {
...
}
...
});
For example, the first time I get to the end of my episodes, I'll have 10 on the page. I want to go out, get 10 more episodes. So I need to increment my count variable to 20 and pass that in as the count param.
The service will return 20 items. The first 10 I want to delete (They are already on screen). I only need the last 10 episodes...
Now I'll have 20 episodes. The next time I scroll, I'll need to increment my count to 30. The service will return an array of 30 items. I will need to delete (splice) the first 20; leaving only the last 10 -- then add that to the episodes array.
The logging should show something like:
this.episodes[10]
this.episodes[20]
this.episodes[30]
I hope that makes sense. I know what I'm trying to achieve, I'm struggling how to actually do it. Thank you for any suggestions!
EDIT/SOLUTION
Thank you so much for the suggestion! In case someone else comes across this, here is what I came up with that is doing exactly what I need.
// load more episodes using infinite scroll.
loadMoreEpisodes(event) {
console.log('--> loading more episodes');
this.count = (this.count + this.count); // 10, 20, 30...
this.myPodcastService.getRssFeed(this.rssUrl, this.count)
.then(data => {
if (data) {
// append the new episodes to the existing array
this.episodes.push(...data.items.splice(-this.episodes.length, this.count));
event.target.complete();
console.log('this.episodes', this.episodes);
} else {
this.alertCtrl.create({
header: 'Error',
subHeader: 'Something bad happened',
message: 'Something internet related happened & we couldn\'t load the playlist.',
buttons: [{ text: 'Ok', role: 'cancel' }]
}).then(alert => {
alert.present();
});
}
});
}
