set form validations

This commit is contained in:
zmagod 2016-09-20 09:50:03 +02:00
parent f48553e807
commit cd64e035c0
2 changed files with 24 additions and 12 deletions

View file

@ -119,31 +119,33 @@
$.ajax({ method: 'GET', $.ajax({ method: 'GET',
url: link, url: link,
beforeSend: animateSpinner(that, true) }) beforeSend: animateSpinner(that, true) })
.done(function(data){ .done(function(data) {
updateCommentHTML(that, data); updateCommentHTML(that, data);
bindCommentButton(); bindCommentButton();
initStepCommentsLink(that); initStepCommentsLink(that);
animateSpinner(that, false); animateSpinner(that, false);
}) })
.always(function(data){ .always(function(data) {
animateSpinner(that, false); animateSpinner(that, false);
}); });
}); });
} }
function refreshComments(child) { function refreshComment(child) {
var parent = child.closest(".step-comment"); var parent = child.closest(".step-comment");
var link = parent.attr("data-href"); var link = parent.attr("data-href");
$.ajax({ method: 'GET', $.ajax({ method: 'GET',
url: link, url: link,
beforeSend: animateSpinner(parent, true) }) beforeSend: animateSpinner(parent, true) })
.done(function(data){ .done(function(data) {
updateCommentHTML(parent, data); updateCommentHTML(parent, data);
bindCommentButton(); bindCommentButton();
initStepCommentsLink(parent); initStepCommentsLink(parent);
animateSpinner(parent, false); animateSpinner(parent, false);
}) })
.always(animateSpinner(parent, false)); .always(function() {
animateSpinner(parent, false);
});
} }
function scrollBottom(id) { function scrollBottom(id) {
@ -163,9 +165,19 @@
} }
function bindCommentAjax(id){ function bindCommentAjax(id){
$(document) $(id)
.on( 'ajax:success', function() { .on('ajax:success', function() {
refreshComments($(id)); refreshComment($(id));
})
.on('ajax:error', function(request, status, error) {
var messageError = status.responseJSON.errors.message.toString();
if (messageError) {
$(request.target)
.addClass("has-error");
$(".help-block", request.target)
.html(messageError)
.removeClass("hide");
}
}); });
} }
@ -173,11 +185,11 @@
function updateCommentHTML(parent, data) { function updateCommentHTML(parent, data) {
var id; var id;
if ( $(parent.find(".comment-form")).attr("id") !== undefined ) { if ( $(parent.find(".comment-form")).attr("id") !== undefined ) {
id = "#" + $(parent.find(".comment-form")).attr("id") id = "#" + $(parent.find(".comment-form")).attr("id");
$(parent.children()[0]).html(data.html); $(parent.find('[data-info="step-comment"]')).html(data.html);
} else { } else {
id = "#" + $( $.parseHTML(data.html) ).find(".comment-form").attr("id"); id = "#" + $( $.parseHTML(data.html) ).find(".comment-form").attr("id");
$(parent.children()[1]).html(data.html); $(parent.find('[data-info="step-comment"]')).html(data.html);
} }
scrollBottom(parent); scrollBottom(parent);
} }

View file

@ -129,7 +129,7 @@
<% if can_view_step_comments(@protocol) %> <% if can_view_step_comments(@protocol) %>
<div class="row"> <div class="row">
<div class="step-comment" data-href="<%= url_for step_step_comments_path(step_id: step.id, format: :json) %>"> <div class="step-comment" data-href="<%= url_for step_step_comments_path(step_id: step.id, format: :json) %>">
<div id="step-comments-<%= step.id %>"> <div id="step-comments-<%= step.id %>" data-info="step-comment">
</div> </div>
</div> </div>
</div> </div>