From 5552cc4d9db05c2cd803e79f0f10232b1cef2e26 Mon Sep 17 00:00:00 2001 From: Antonin Date: Mon, 26 Oct 2015 16:46:08 +0100 Subject: [PATCH] studs.js refactoring. - Replacing "" by '' - Use chaining when possible - Correct the scrolling on comment correctly set. --- js/app/studs.js | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/js/app/studs.js b/js/app/studs.js index 6245c97..bb0c89b 100644 --- a/js/app/studs.js +++ b/js/app/studs.js @@ -18,18 +18,19 @@ $(document).ready(function () { - $("#poll_form").submit(function (event) { - var name = $("#name").val(); - name = name.trim(); + $('#poll_form').submit(function (event) { + var name = $('#name').val().trim(); if (name.length == 0) { event.preventDefault(); - var newMessage = $("#nameErrorMessage").clone(); - $("#message-container").empty(); - $("#message-container").append(newMessage); - newMessage.removeClass("hidden"); + var newMessage = $('#nameErrorMessage').clone(); + var messageContainer = $('#message-container'); + messageContainer + .empty() + .append(newMessage); + newMessage.removeClass('hidden'); $('html, body').animate({ - scrollTop: $("#message-container").offset().top + scrollTop: messageContainer.offset().top }, 750); } }); @@ -45,30 +46,40 @@ $(document).ready(function () { }); var form = $('#comment_form'); - form.submit(function(event) { event.preventDefault(); $.ajax({ - type: "POST", + type: 'POST', url: form.attr('action'), data: form.serialize(), dataType: 'json', success: function(data) { - $('#comment').val(""); + $('#comment').val(''); if (data.result) { - $("#comments_list").replaceWith(data.comments); - var lastComment = $("#comments_list").find("div.comment").last(); + $('#comments_list') + .replaceWith(data.comments); + var lastComment = $('#comments_list') + .find('div.comment') + .last(); lastComment.effect('highlight', {color: 'green'}, 401); $('html, body').animate({ - scrollTop: $("#comments_alerts").offset().top + scrollTop: lastComment.offset().top }, 750); } else { - var newMessage = $("#genericErrorTemplate").clone(); - newMessage.find(".contents").text(data.message.message); - $("#comments_alerts").empty().append(newMessage); + 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); } } });