2012-04-05 11:21:49 +00:00
|
|
|
/*\
|
|
|
|
title: js/HttpSync.js
|
|
|
|
|
2012-04-07 10:32:16 +00:00
|
|
|
A very simple synchroniser. It PUTs updated or created tiddlers, and DELETEs deleted tiddlers.
|
|
|
|
|
2012-04-05 11:21:49 +00:00
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
2012-04-05 17:25:39 +00:00
|
|
|
/*jslint node: true, browser: true */
|
2012-04-05 11:21:49 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function HttpSync(store) {
|
|
|
|
this.store = store;
|
|
|
|
this.changeCounts = {};
|
|
|
|
store.addEventListener("",function(changes) {
|
|
|
|
for(var title in changes) {
|
2012-04-05 17:25:39 +00:00
|
|
|
var x = new XMLHttpRequest(),
|
|
|
|
tiddler = store.getTiddler(title);
|
2012-04-05 11:21:49 +00:00
|
|
|
if(tiddler) {
|
|
|
|
var fieldStrings = tiddler.getFieldStrings(),
|
|
|
|
fields = {},
|
|
|
|
t;
|
|
|
|
for(t=0; t<fieldStrings.length; t++) {
|
|
|
|
fields[fieldStrings[t].name] = fieldStrings[t].value;
|
|
|
|
}
|
|
|
|
fields.text = tiddler.text;
|
|
|
|
x.open("PUT",window.location.toString() + encodeURIComponent(title),true);
|
|
|
|
x.setRequestHeader("Content-type", "application/json");
|
|
|
|
x.send(JSON.stringify(fields));
|
2012-04-05 17:25:39 +00:00
|
|
|
} else {
|
|
|
|
x.open("DELETE",window.location.toString() + encodeURIComponent(title),true);
|
|
|
|
x.send();
|
2012-04-05 11:21:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.HttpSync = HttpSync;
|
|
|
|
|
|
|
|
})();
|