studs.js refactoring.

- Replacing "" by ''
    - Use chaining when possible
    - Correct the scrolling on comment correctly set.
This commit is contained in:
Antonin 2015-10-26 16:46:08 +01:00
parent 974148550b
commit 5552cc4d9d

View File

@ -18,18 +18,19 @@
$(document).ready(function () { $(document).ready(function () {
$("#poll_form").submit(function (event) { $('#poll_form').submit(function (event) {
var name = $("#name").val(); var name = $('#name').val().trim();
name = name.trim();
if (name.length == 0) { if (name.length == 0) {
event.preventDefault(); event.preventDefault();
var newMessage = $("#nameErrorMessage").clone(); var newMessage = $('#nameErrorMessage').clone();
$("#message-container").empty(); var messageContainer = $('#message-container');
$("#message-container").append(newMessage); messageContainer
newMessage.removeClass("hidden"); .empty()
.append(newMessage);
newMessage.removeClass('hidden');
$('html, body').animate({ $('html, body').animate({
scrollTop: $("#message-container").offset().top scrollTop: messageContainer.offset().top
}, 750); }, 750);
} }
}); });
@ -45,30 +46,40 @@ $(document).ready(function () {
}); });
var form = $('#comment_form'); var form = $('#comment_form');
form.submit(function(event) { form.submit(function(event) {
event.preventDefault(); event.preventDefault();
$.ajax({ $.ajax({
type: "POST", type: 'POST',
url: form.attr('action'), url: form.attr('action'),
data: form.serialize(), data: form.serialize(),
dataType: 'json', dataType: 'json',
success: function(data) success: function(data)
{ {
$('#comment').val(""); $('#comment').val('');
if (data.result) { if (data.result) {
$("#comments_list").replaceWith(data.comments); $('#comments_list')
var lastComment = $("#comments_list").find("div.comment").last(); .replaceWith(data.comments);
var lastComment = $('#comments_list')
.find('div.comment')
.last();
lastComment.effect('highlight', {color: 'green'}, 401); lastComment.effect('highlight', {color: 'green'}, 401);
$('html, body').animate({ $('html, body').animate({
scrollTop: $("#comments_alerts").offset().top scrollTop: lastComment.offset().top
}, 750); }, 750);
} else { } else {
var newMessage = $("#genericErrorTemplate").clone(); var newMessage = $('#genericErrorTemplate').clone();
newMessage.find(".contents").text(data.message.message); newMessage
$("#comments_alerts").empty().append(newMessage); .find('.contents')
.text(data.message.message);
var commentsAlert = $('#comments_alerts');
commentsAlert
.empty()
.append(newMessage);
$('html, body').animate({
scrollTop: commentsAlert.offset().top
}, 750);
} }
} }
}); });