9. CACHE MANIFEST
# 2013-10-31:v3
# resources to cache
CACHE:
index.html
css/style.css
scripts/app.js
# offline.html will be displayed if the user is offline
FALLBACK:
img/ img/offline.jpg
/ /offline.html
# All other resources (e.g. sites) require the user to be online.
NETWORK:
*
11. // Check if a new cache is available on page load.
window.addEventListener(
'load', function(e) {
window.applicationCache.addEventListener(
'updateready', function
(e){
if (window.applicationCache.status == window.
applicationCache.UPDATEREADY) {
// Browser downloaded a new app cache.
if (confirm('A new version is available. Load it?' {
))
window.location.reload();
}
} else {
// Manifest didn't changed. Nothing new to server.
}
}, false);
}, false);
23. // In localStorage, we would do:
localStorage.setItem('key', JSON.stringify(value));
doSomethingElse();
// With localForage, we use callbacks:
localForage.setItem('key', value, doSomethingElse);
24. // Synchronous; slower!
var value = JSON.parse(localStorage.getItem('key'));
alert(value);
// Async, fast, and non-blocking!
localForage.getItem('key', alert);
25. Device Storage API
(only firefoxOS)
●
●
●
●
accessing file system
only for privileged and certified apps
asynchronous
slow!
28. var sdcard = navigator.getDeviceStorage("sdcard");
var file
= new Blob(["This is a text file."], type:"
text/plain"});
var request = sdcard.addNamed(file, "my-file.txt");
request.onsuccess = function () {
var name = this.result;
console.log('wrote to sdcard"' + name);
}
// an error typically occur if a file with same name already
exist
request.onerror = function () {
console.warn('Unable to write the file: ' + this.error);
}
29. var sdcard = navigator.getDeviceStorage("sdcard");
var request = sdcard.get("my-file.txt");
request.onsuccess = function () {
var file = this.result;
console.log('got file"' + file.name);
}
request.onerror = function () {
console.warn('Unable to get the file: ' + this.error);
}
30. available space
● freeSpace()
● usedSpace()
var request = sdcard.freeSpace();
request.onsuccess = function () {
console.log(“available space in bytes:”+this.result)
}