|
|
@@ -1,11 +1,46 @@
|
|
|
var adapter = {
|
|
|
server_url: "",
|
|
|
responseJson: {},
|
|
|
- onset: function() {},
|
|
|
- onget: function() {},
|
|
|
+ onset: function () {},
|
|
|
+ onget: function () {},
|
|
|
+ differ: function (base, object) {
|
|
|
+ base = JSON.parse(JSON.stringify(base));
|
|
|
+ object = JSON.parse(JSON.stringify(object));
|
|
|
+
|
|
|
+ console.log('INPUT', base, object);
|
|
|
+
|
|
|
+ function difference(o1, o2) {
|
|
|
+ var k, kDiff,
|
|
|
+ diff = {};
|
|
|
+ for (k in o1) {
|
|
|
+ if (!o1.hasOwnProperty(k)) {
|
|
|
+ } else if (typeof o1[k] != 'object' || typeof o2[k] != 'object') {
|
|
|
+ if (!(k in o2) || o1[k] !== o2[k]) {
|
|
|
+ diff[k] = o2[k];
|
|
|
+ }
|
|
|
+ } else if (kDiff = difference(o1[k], o2[k])) {
|
|
|
+ diff[k] = kDiff;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (k in o2) {
|
|
|
+ if (o2.hasOwnProperty(k) && !(k in o1)) {
|
|
|
+ diff[k] = o2[k];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (k in diff) {
|
|
|
+ if (diff.hasOwnProperty(k)) {
|
|
|
+ return diff;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ return difference(base, object);
|
|
|
+ },
|
|
|
setter: function () {
|
|
|
+
|
|
|
+ var diff = this.differ(this.responseJson, this.onset());
|
|
|
xhr = new XMLHttpRequest();
|
|
|
- xhr.open('GET', this.server_url + '/set/' + encodeURIComponent(JSON.stringify(this.onset())));
|
|
|
+ xhr.open('GET', this.server_url + '/set/' + encodeURIComponent(JSON.stringify(diff)));
|
|
|
xhr.onload = function () {
|
|
|
console.log(xhr.responseText);
|
|
|
};
|
|
|
@@ -17,9 +52,10 @@ var adapter = {
|
|
|
xhr.open('GET', this.server_url + '/get');
|
|
|
xhr.onload = function () {
|
|
|
try {
|
|
|
- var response = JSON.parse(this.responseText);
|
|
|
- self.responseJson = response;
|
|
|
+ console.log(JSON.parse(this.responseText));
|
|
|
+ self.responseJson = JSON.parse(this.responseText);
|
|
|
self.onget();
|
|
|
+ console.log('loaded');
|
|
|
} catch (err) {
|
|
|
console.warn("json could not be parsed:", err, this.responseText);
|
|
|
}
|