Skip to content

Instantly share code, notes, and snippets.

@ritikrishu
Last active March 11, 2018 20:20
Show Gist options
  • Save ritikrishu/57655db0349623dc544b1b79fe955049 to your computer and use it in GitHub Desktop.
Save ritikrishu/57655db0349623dc544b1b79fe955049 to your computer and use it in GitHub Desktop.
using cache.addAll in DOM event handler
/**
purpose:
example is for a music player app, where we want to store
selective playlists for offline usage
on user click to `Save Offline` button, we want to add all
songs in playlist to the cache storage
*/
document.getElementBy('saveOfflineBtn').addEventListener('click', function(event) {
event.preventDefault();
const id = this.currentPlaylist.id;
caches.open('songs-cache').then(function(cache) {
fetch(`/get-all-songs-url-from-playlist?id=${id}`).then(function(response) {
// response is a JSON-encoded array of
// songs URL that a given playlist has
return response.json();
}).then(function(urls) {
cache.addAll(urls);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment