use promise lib correctly

This commit is contained in:
Martin Zimmermann 2013-10-03 13:47:38 +02:00
parent e4f9b53668
commit 57a16acff4
2 changed files with 58 additions and 56 deletions

View File

@ -7,14 +7,21 @@ define(["q"], function(Q) {
"use strict"; "use strict";
// http://stackoverflow.com/questions/17544965/unhandled-rejection-reasons-should-be-empty Q.stopUnhandledRejectionTracking();
// Q.stopUnhandledRejectionTracking();
Q.longStackSupport = true; Q.longStackSupport = true;
var endpoint = null, remote_addr = null, var endpoint = null, remote_addr = null,
salt = "Eech7co8Ohloopo9Ol6baimi", salt = "Eech7co8Ohloopo9Ol6baimi",
location = window.location.pathname; location = window.location.pathname;
var rules = {
"/": [200, 404],
"/new": [201, 202],
"/id/\\d+": [200, 403, 404],
"/id/\\d+/(like/dislike)": [200],
"/count": [200]
};
// guess Isso API location // guess Isso API location
var js = document.getElementsByTagName("script"); var js = document.getElementsByTagName("script");
for (var i = 0; i < js.length; i++) { for (var i = 0; i < js.length; i++) {
@ -36,13 +43,15 @@ define(["q"], function(Q) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
var response = Q.defer(); var response = Q.defer();
if (! ("withCredentials" in xhr)) {
respone.reject("I won't support IE ≤ 10.");
return response.promise;
}
function onload() { function onload() {
response.resolve({status: xhr.status, body: xhr.responseText});
var rule = url.replace(endpoint, "").split("?", 1)[0];
if (rule in rules && rules[rule].indexOf(xhr.status) === -1) {
response.reject(xhr.responseText);
} else {
response.resolve({status: xhr.status, body: xhr.responseText});
}
} }
try { try {
@ -70,19 +79,11 @@ define(["q"], function(Q) {
} }
return rv.substring(0, rv.length - 1); // chop off trailing "&" return rv.substring(0, rv.length - 1); // chop off trailing "&"
} };
var create = function(data) { var create = function(data) {
return curl("POST", endpoint + "/new?" + qs({uri: location}), JSON.stringify(data)).then(
return curl("POST", endpoint + "/new?" + qs({uri: location}), JSON.stringify(data)) function (rv) { return JSON.parse(rv.body); });
.then(function (rv) {
if (rv.status === 201 || rv.status === 202) {
return JSON.parse(rv.body);
} else {
var msg = rv.body.match("<p>(.+)</p>");
throw {status: rv.status, reason: (msg && msg[1]) || rv.body};
}
});
}; };
var modify = function(data) { var modify = function(data) {
@ -90,63 +91,58 @@ define(["q"], function(Q) {
}; };
var remove = function(id) { var remove = function(id) {
return curl("DELETE", endpoint + "/id/" + id, null) return curl("DELETE", endpoint + "/id/" + id, null).then(function(rv) {
.then(function(rv) { if (rv.status === 403) {
if (rv.status === 200) { throw "Not authorized to remove this comment!";
return JSON.parse(rv.body) === null;
} else {
throw {status: rv.status, reason: rv.body};
} }
return JSON.parse(rv.body) === null;
}); });
}; };
var fetch = function() { var fetch = function() {
return curl("GET", endpoint + "/?" + qs({uri: location}), null) return curl("GET", endpoint + "/?" + qs({uri: location}), null).then(function (rv) {
.then(function (rv) {
if (rv.status === 200) { if (rv.status === 200) {
return JSON.parse(rv.body); return JSON.parse(rv.body);
} else { } else {
var msg = rv.body.match("<p>(.+)</p>"); return [];
throw {status: rv.status, reason: (msg && msg[1]) || rv.body};
} }
}); });
}; };
var count = function(uri) { var count = function(uri) {
return curl("GET", endpoint + "/count?" + qs({uri: uri}), null) return curl("GET", endpoint + "/count?" + qs({uri: uri}), null).then(function(rv) {
.then(function (rv) { return JSON.parse(rv.body);
if (rv.status == 200) });
return JSON.parse(rv.body) };
throw {status: rv.status, reason: rv.body};
})
}
var like = function(id) { var like = function(id) {
return curl("POST", endpoint + "/id/" + id + "/like", null) return curl("POST", endpoint + "/id/" + id + "/like", null).then(function(rv) {
.then(function(rv) {
return JSON.parse(rv.body); return JSON.parse(rv.body);
}) });
} };
var dislike = function(id) { var dislike = function(id) {
return curl("POST", endpoint + "/id/" + id + "/dislike", null) return curl("POST", endpoint + "/id/" + id + "/dislike", null).then(function(rv) {
.then(function(rv) { return JSON.parse(rv.body);
return JSON.parse(rv.body); });
}) };
}
remote_addr = curl("GET", endpoint + "/check-ip", null).then(function(rv) {return rv.body}); remote_addr = curl("GET", endpoint + "/check-ip", null).then(function(rv) {
return rv.body;
});
return { return {
endpoint: endpoint, remote_addr: remote_addr, salt: salt, endpoint: endpoint,
remote_addr: remote_addr,
salt: salt,
create: create, create: create,
remove: remove, remove: remove,
fetch: fetch, fetch: fetch,
count: count, count: count,
like: like, like: like,
dislike: dislike dislike: dislike
} };
}); });

View File

@ -15,13 +15,19 @@ require(["ready", "app/api", "app/isso", "app/count", "app/dom", "app/markup"],
$("#isso-thread").append(new isso.Postbox(null)); $("#isso-thread").append(new isso.Postbox(null));
$("#isso-thread").append('<div id="isso-root"></div>'); $("#isso-thread").append('<div id="isso-root"></div>');
api.fetch().then(function(comments) { api.fetch().then(function(rv) {
$("#isso-thread > h4").textContent = Mark.up("{{ i18n-num-comments | pluralize : `n` }}", {n: comments.length});
for (var i=0; i < comments.length; i++) { if (! rv.length) {
isso.insert(comments[i], false); $("#isso-thread > h4").textContent = Mark.up("{{ i18n-no-comments }}");
return;
} }
}).fail(function() {
$("#isso-thread > h4").textContent = Mark.up("{{ i18n-no-comments }}"); $("#isso-thread > h4").textContent = Mark.up("{{ i18n-num-comments | pluralize : `n` }}", {n: rv.length});
for (var i=0; i < rv.length; i++) {
isso.insert(rv[i], false);
}
}).fail(function(err) {
console.log(err);
}); });
}); });
}); });