navigator.onLine
// true | false
if(navigator.onLine){
// …
}else{
// …
}
window.addEventListener('online', callback);
window.document.addEventListener('online', callback);
window.document.body.addEventListener('online', callback);
window.addEventListener('offline', callback);
window.document.addEventListener('offline', callback);
window.document.body.addEventListener('offline', callback);
var form = $('#comment-form'),
messages = [];
form.on('submit', function (event) {
event.preventDefault();
if(navigator.onLine){
$.post(
form.attr('action'),
form.serialize()
);
}else{
messages.push(form.serialize());
}
form[0].reset();
});
$(window).on('online', function () {
while(messages.length>0){
$.post(
form.attr('action'),
messages.shift()
);
}
});
var form = $('#comment-form'),
messages = JSON.parse(localStorage.getItem('messages')) || [];
form.on('submit', function (event) {
event.preventDefault();
if(navigator.onLine){
$.post(form.attr('action'), form.serialize());
}else{
messages.push(form.serialize());
localStorage.setItem('messages',
JSON.stringify(messages));
}
form[0].reset();
});
if(navigator.onLine){
while(messages.length>0){
$.post(form.attr('action'), messages.shift());
}
localStorage.removeItem('messages');
}
$(window).on('online', function () {
while(messages.length>0){
$.post(form.attr('action'), messages.shift());
}
localStorage.removeItem('messages');
});
bower install --save offline
npm install --save offline-js
<link
rel="stylesheet"
href="themes/offline-theme-chrome.css">
<link
rel="stylesheet"
href="themes/offline-language-english.css">
<script src="offline.min.js"></script>
Offline.options = {
checks: {…},
checkmOnLoad: true,
interceptRequests: true,
requests: true,
reconnect: {…}
};
Offline.check();
Offline.state;
Offline.on(
event,
handler,
context);
Offline.off(
event,
handler);
up
down
confirmed-up
confirmed-down
checking
…
npm install -g hoodie-cli
hoodie new
cd my-first-hoodie/
hoodie start
<script src="jquery.min.js"></script>
<script src="/_api/_files/hoodie.js"></script>
hoodie
hoodie.account
hoodie.store
hoodie.account
.signUp(username, password)
.done(success_callback)
.fail(fail_callback);
hoodie.account
.signIn(username, password)
.done(success_callback)
.fail(fail_callback);
npm install --save swarm
let swarm = require('swarm'),
Text = require('swarm/lib/Text'),
fs = new swarm.FileStorage('id'),
host = new swarm.Host(
'swarm~nodejs',
0,
fs
),
app = new Text('APP');
ws_server.on('connection', function (ws) {
host.accept(
new swarm.EinarosWSStream(ws),
{delay: 50}
);
});
let swarm = require('swarm'),
Text = require('swarm/lib/Text'),
host = new swarm.Host('id');
this.model = new Text('APP');
this.model.on(
this.eventMonitor.bind(this));
host.connect(
'ws://127.0.0.1:8000/');
window.addEventListener(
'onbeforeunload',
this.turnOff.bind(this));
eventMonitor (spec, val, source) {
if (spec.op() == 'init') {
// Инициализировался
} else {
// Изменился
}
}
this.model.text
this.model.set(value);
host.close();
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register(
'worker.js',
{scope: '/'})
.then(success)
.catch(error);
}
// События
install
activate
fetch
message
this.addEventListener(
'fetch',
function(event) {});
event.request
event.request.url
event.respondWith(
new Response('Owned!')
);
caches.open('id')
.then(function(cache) {
return cache.addAll([…]);
});
event.respondWith(
caches.open('id')
.then(function(cache) {
return cache.match(event.request)
.then(function(response) {…}
});
});
if (response) {
return response;
} else {
cache.add(event.request.url);
return fetch(event.request);
}
// client.js
let message = {
'command': 'flush'
};
navigator
.serviceWorker
.controller
.postMessage(message);
// worker.js
switch (event.data.command) {
case 'flush':
return caches.delete('id');
break;
}
// Chrome
chrome://inspect/#service-workers
// Opera
browser://serviceworker-internals
// Firefox
about:serviceworkers
Антон Немцев
http://silentimp.info/
@silentimp
thesilentimp@gmail.com
skype: ravencry