28 lines
922 B
JavaScript
28 lines
922 B
JavaScript
function handleAjaxResponse($el, callback) {
|
|
var $header = $('header');
|
|
$el.on("ajax:success", function(e, data){
|
|
$header.after('<div class="success">' + data.message + '</div>');
|
|
callback(data);
|
|
}).on("ajax:error", function(e, xhr) {
|
|
if (xhr.status === 400){
|
|
$header.after('<div class="error">' + xhr.responseJSON.join('<br>') + '</div>');
|
|
} else {
|
|
$header.after('<div class="error">Oops! There was an error processing your request. Please try again.</div>');
|
|
}
|
|
});
|
|
}
|
|
|
|
function updateVotes(data){
|
|
$("[data-id=up-votes]").html(data.upCount);
|
|
$("[data-id=down-votes]").html(data.downCount);
|
|
$("[data-id=my-vote]").html(data.myVote);
|
|
}
|
|
|
|
$(document).ready(function() {
|
|
$('[data-id=ajax-action]').each(function(){ handleAjaxResponse($(this)); });
|
|
});
|
|
|
|
$(document).ready(function() {
|
|
$('[data-id=vote-count]').each(function(){ handleAjaxResponse($(this), updateVotes); });
|
|
});
|