skill-assessment-app/app/assets/javascripts/main/ajax-links.js

28 lines
922 B
JavaScript
Raw Normal View History

2016-11-20 11:24:17 -06:00
function handleAjaxResponse($el, callback) {
2016-09-15 10:01:31 -05:00
var $header = $('header');
$el.on("ajax:success", function(e, data){
$header.after('<div class="success">' + data.message + '</div>');
2016-11-20 11:24:17 -06:00
callback(data);
2016-09-15 10:01:31 -05:00
}).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>');
}
});
}
2016-11-20 11:24:17 -06:00
function updateVotes(data){
$("[data-id=up-votes]").html(data.upCount);
$("[data-id=down-votes]").html(data.downCount);
$("[data-id=my-vote]").html(data.myVote);
}
2016-09-15 10:01:31 -05:00
$(document).ready(function() {
$('[data-id=ajax-action]').each(function(){ handleAjaxResponse($(this)); });
});
2016-11-20 11:24:17 -06:00
$(document).ready(function() {
$('[data-id=vote-count]').each(function(){ handleAjaxResponse($(this), updateVotes); });
});