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;
|
return deferred.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
var like = function(id) {
|
var vote = function(type) {
|
||||||
|
return function(id) {
|
||||||
var deferred = Q.defer();
|
var deferred = Q.defer();
|
||||||
curl("POST", endpoint + "/id/" + id + "/like", null,
|
curl("POST", endpoint + "/id/" + id + "/" + type, null, function(rv) {
|
||||||
function(rv) { deferred.resolve(JSON.parse(rv.body)); });
|
if (rv.status === 200) {
|
||||||
|
return deferred.resolve(null);
|
||||||
|
} else {
|
||||||
|
return deferred.reject("Unable to vote!");
|
||||||
|
}
|
||||||
|
});
|
||||||
return deferred.promise;
|
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -193,7 +193,7 @@ define(["app/lib/promise", "app/globals"], function(Q, globals) {
|
|||||||
view: view,
|
view: view,
|
||||||
fetch: fetch,
|
fetch: fetch,
|
||||||
count: count,
|
count: count,
|
||||||
like: like,
|
like: vote("like"),
|
||||||
dislike: dislike
|
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 vote = (function() {
|
||||||
var votes = function(value) {
|
var span = $("span.votes", footer),
|
||||||
var span = $("span.votes", footer);
|
value = span === null ? 0 : parseInt(span.textContent, 10);
|
||||||
if (span === null && value !== 0) {
|
|
||||||
footer.prepend($.new("span.votes", value));
|
return function(n) {
|
||||||
} else {
|
value += n;
|
||||||
|
|
||||||
if (value === 0) {
|
if (value === 0) {
|
||||||
|
if (span !== null) {
|
||||||
span.remove();
|
span.remove();
|
||||||
|
span = null;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (span === null) {
|
||||||
|
span = footer.prepend($.new("span.votes"));
|
||||||
|
}
|
||||||
span.textContent = value;
|
span.textContent = value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
}());
|
||||||
|
|
||||||
$("a.upvote", footer).on("click", function() {
|
$("a.upvote", footer).on("click", function() {
|
||||||
api.like(comment.id).then(function(rv) {
|
api.like(comment.id).then(function() {
|
||||||
votes(rv.likes - rv.dislikes);
|
vote(+1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$("a.downvote", footer).on("click", function() {
|
$("a.downvote", footer).on("click", function() {
|
||||||
api.dislike(comment.id).then(function(rv) {
|
api.dislike(comment.id).then(function() {
|
||||||
votes(rv.likes - rv.dislikes);
|
vote(-1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user