new API response for like/dislike
This commit is contained in:
parent
bd1cb498d1
commit
6245a8dc17
@ -169,18 +169,18 @@ define(["app/lib/promise", "app/globals"], function(Q, globals) {
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
var like = function(id) {
|
||||
var deferred = Q.defer();
|
||||
curl("POST", endpoint + "/id/" + id + "/like", null,
|
||||
function(rv) { deferred.resolve(JSON.parse(rv.body)); });
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
var dislike = function(id) {
|
||||
var deferred = Q.defer();
|
||||
curl("POST", endpoint + "/id/" + id + "/dislike", null,
|
||||
function(rv) { deferred.resolve(JSON.parse(rv.body)); });
|
||||
return deferred.promise;
|
||||
var vote = function(type) {
|
||||
return function(id) {
|
||||
var deferred = Q.defer();
|
||||
curl("POST", endpoint + "/id/" + id + "/" + type, null, function(rv) {
|
||||
if (rv.status === 200) {
|
||||
return deferred.resolve(null);
|
||||
} else {
|
||||
return deferred.reject("Unable to vote!");
|
||||
}
|
||||
});
|
||||
return deferred.promise;
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
@ -193,7 +193,7 @@ define(["app/lib/promise", "app/globals"], function(Q, globals) {
|
||||
view: view,
|
||||
fetch: fetch,
|
||||
count: count,
|
||||
like: like,
|
||||
dislike: dislike
|
||||
like: vote("like"),
|
||||
dislike: vote("dislike")
|
||||
};
|
||||
});
|
||||
|
@ -144,29 +144,36 @@ define(["app/dom", "app/utils", "app/config", "app/api", "app/jade", "app/i18n",
|
||||
}
|
||||
);
|
||||
|
||||
// update vote counter, but hide if votes sum to 0
|
||||
var votes = function(value) {
|
||||
var span = $("span.votes", footer);
|
||||
if (span === null && value !== 0) {
|
||||
footer.prepend($.new("span.votes", value));
|
||||
} else {
|
||||
var vote = (function() {
|
||||
var span = $("span.votes", footer),
|
||||
value = span === null ? 0 : parseInt(span.textContent, 10);
|
||||
|
||||
return function(n) {
|
||||
value += n;
|
||||
|
||||
if (value === 0) {
|
||||
span.remove();
|
||||
if (span !== null) {
|
||||
span.remove();
|
||||
span = null;
|
||||
}
|
||||
} else {
|
||||
if (span === null) {
|
||||
span = footer.prepend($.new("span.votes"));
|
||||
}
|
||||
span.textContent = value;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}());
|
||||
|
||||
$("a.upvote", footer).on("click", function() {
|
||||
api.like(comment.id).then(function(rv) {
|
||||
votes(rv.likes - rv.dislikes);
|
||||
api.like(comment.id).then(function() {
|
||||
vote(+1);
|
||||
});
|
||||
});
|
||||
|
||||
$("a.downvote", footer).on("click", function() {
|
||||
api.dislike(comment.id).then(function(rv) {
|
||||
votes(rv.likes - rv.dislikes);
|
||||
api.dislike(comment.id).then(function() {
|
||||
vote(-1);
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user