Last active
March 11, 2018 20:20
-
-
Save ritikrishu/57655db0349623dc544b1b79fe955049 to your computer and use it in GitHub Desktop.
using cache.addAll in DOM event handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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