From 1ec9ea2dd5bc5f1cb7b5dbb3fff8a5ed435135b3 Mon Sep 17 00:00:00 2001 From: Antonin Date: Wed, 28 Oct 2015 16:36:54 +0100 Subject: [PATCH] Sugar for comment sending. - Do not send if comment empty - Disable submit button while submit is in progress --- js/app/studs.js | 71 ++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/js/app/studs.js b/js/app/studs.js index bb0c89b..6a4b300 100644 --- a/js/app/studs.js +++ b/js/app/studs.js @@ -49,40 +49,45 @@ $(document).ready(function () { form.submit(function(event) { event.preventDefault(); - $.ajax({ - type: 'POST', - url: form.attr('action'), - data: form.serialize(), - dataType: 'json', - success: function(data) - { - $('#comment').val(''); - - if (data.result) { - $('#comments_list') - .replaceWith(data.comments); - var lastComment = $('#comments_list') - .find('div.comment') - .last(); - lastComment.effect('highlight', {color: 'green'}, 401); - $('html, body').animate({ - scrollTop: lastComment.offset().top - }, 750); - } else { - var newMessage = $('#genericErrorTemplate').clone(); - newMessage - .find('.contents') - .text(data.message.message); - var commentsAlert = $('#comments_alerts'); - commentsAlert - .empty() - .append(newMessage); - $('html, body').animate({ - scrollTop: commentsAlert.offset().top - }, 750); + if ($('#comment').val()) { + $('#add_comment').attr("disabled", "disabled"); + $.ajax({ + type: 'POST', + url: form.attr('action'), + data: form.serialize(), + dataType: 'json', + success: function(data) + { + $('#comment').val(''); + if (data.result) { + $('#comments_list') + .replaceWith(data.comments); + var lastComment = $('#comments_list') + .find('div.comment') + .last(); + lastComment.effect('highlight', {color: 'green'}, 401); + $('html, body').animate({ + scrollTop: lastComment.offset().top + }, 750); + } else { + var newMessage = $('#genericErrorTemplate').clone(); + newMessage + .find('.contents') + .text(data.message.message); + var commentsAlert = $('#comments_alerts'); + commentsAlert + .empty() + .append(newMessage); + $('html, body').animate({ + scrollTop: commentsAlert.offset().top + }, 750); + } + }, + complete: function() { + $('#add_comment').removeAttr("disabled"); } - } - }); + }); + } return false; });