Cache API
:这是存储URL地址资源的最佳选择。和Service worker配合是非常好滴。
PouchDB
:是
CouchDB
的开源JavaScript数据库。它使应用程序能够在本地存储数据,离线,然后同步与CouchDB和兼容的服务器应用程序时重新上线,保持用户数据同步,不管他们下一次在哪里登录。PouchDB支持所有现代浏览器,使用IndexedDB引擎失败的话就降级到WebSQL,对Firefox 29+ (包括 Firefox OS and Firefox for Android), Chrome 30+, Safari 5+, Internet Explorer 10+, Opera 21+, Android 4.0+, iOS 7.1+ 和 Windows Phone 8+等等都是兼容的。
// Get the commits Data from the Web Storage
function fetchCommitsFromLocalStorage(data) {
var localData = JSON.parse(data);
app.spinner.setAttribute('hidden', true); //hide spinner
for (var i = 0; i < commitContainer.length; i++) {
container.querySelector("" + commitContainer[i]).innerHTML =
"<h4> Message: " + localData[posData[i]].message + "</h4>" +
"<h4> Author: " + localData[posData[i]].author + "</h4>" +
"<h4> Time committed: " + (new Date(localData[posData[i]].time)).toUTCString() + "</h4>" +
"<h4>" + "<a href='" + localData[posData[i]].link + "'>Click me to see more!</a>" + "</h4>";
}
};
(function() {
'use strict';
var app = {
spinner: document.querySelector('.loader')
};
var container = document.querySelector('.container');
var commitContainer = ['.first', '.second', '.third', '.fourth', '.fifth'];
var posData = ['first', 'second', 'third', 'fourth', 'fifth'];
// Check that localStorage is both supported and available
function storageAvailable(type) {
try {
var storage = window[type],
x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
returntrue;
}
catch(e) {
returnfalse;
}
}
// Get Commit Data from Github API
functionfetchCommits() {
var url = 'https://api.github.com/repos/unicodeveloper/resources-i-like/commits';
fetch(url)
.then(function(fetchResponse){
return fetchResponse.json();
})
.then(function(response) {
console.log("Response from Github", response);
var commitData = {};
for (var i = 0; i < posData.length; i++) {
commitData[posData[i]] = {
message: response[i].commit.message,
author: response[i].commit.author.name,
time: response[i].commit.author.date,
link: response[i].html_url
};
}
localStorage.setItem('commitData', JSON.stringify(commitData));
for (var i = 0; i < commitContainer.length; i++) {
container.querySelector("" + commitContainer[i]).innerHTML =
"<h4> Message: " + response[i].commit.message + "</h4>" +
"<h4> Author: " + response[i].commit.author.name + "</h4>" +
"<h4> Time committed: " + (new Date(response[i].commit.author.date)).toUTCString() + "</h4>" +
"<h4>" + "<a href='" + response[i].html_url + "'>Click me to see more!</a>" + "</h4>";
}
app.spinner.setAttribute('hidden', true); // hide spinner
})
.catch(function (error) {
console.error(error);
});
};
// Get the commits Data from the Web Storage
function fetchCommitsFromLocalStorage(data) {
var localData = JSON.parse(data);
app.spinner.setAttribute('hidden', true); //hide spinner
for (var i = 0; i < commitContainer.length; i++) {
container.querySelector("" + commitContainer[i]).innerHTML =
"<h4> Message: " + localData[posData[i]].message + "</h4>" +
"<h4> Author: " + localData[posData[i]].author + "</h4>" +
"<h4> Time committed: " + (new Date(localData[posData[i]].time)).toUTCString() + "</h4>" +
"<h4>" + "<a href='" + localData[posData[i]].link + "'>Click me to see more!</a>" + "</h4>";
}
};
if (storageAvailable('localStorage')) {
if (localStorage.getItem('commitData') === null) {
/* The user is using the app for the first time, or the user has not
* saved any commit data, so show the user some fake data.
*/
fetchCommits();
console.log("Fetch from API");
} else {
fetchCommitsFromLocalStorage(local