Last active
August 29, 2015 14:07
-
-
Save redgeoff/0c43bc7391aa0fb8c534 to your computer and use it in GitHub Desktop.
PouchDB Chatty
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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="//cdn.jsdelivr.net/pouchdb/3.0.5/pouchdb.js"></script> | |
</head> | |
<body> | |
<script> | |
var db = new PouchDB('test_projects'), changes = null, to = null, from = null; | |
function onError(err) { | |
console.log(err); | |
} | |
db.info().then(function (info) { | |
var remoteCouch = 'https://pouchyng.iriscouch.com/projects', | |
opts = { live: true }; | |
changes = db.changes({ | |
since: info.update_seq, | |
live: true | |
}); | |
registerListeners(); | |
to = db.replicate.to(remoteCouch, opts, onError); | |
from = db.replicate.from(remoteCouch, opts, onError); | |
from.on('uptodate', fnFactory('uptodate')) | |
.on('complete', fnFactory('complete')) | |
.on('error', fnFactory('error')); | |
}); | |
function fnFactory(str) { | |
return function () { | |
console.log(str); | |
}; | |
} | |
function registerListeners() { | |
db.on('create', fnFactory('create')) | |
.on('update', fnFactory('update')) | |
.on('delete', fnFactory('delete')); | |
} | |
// Cancel replication for easier debugging | |
function cancelReplication() { | |
changes.cancel(); | |
to.cancel(); | |
from.cancel(); | |
} | |
</script> | |
<button onclick="cancelReplication()">Cancel Replication</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment