scinote-web/app/assets/javascripts/step_comments.js

64 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-02-12 23:52:43 +08:00
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2016-09-15 14:46:14 +08:00
(function(){
function initializeComments(){
var steps = $(".step-comment");
$.each(steps, function(){
var that = $(this);
var link = that.attr("data-href");
$.ajax({
method: 'GET',
url: link,
beforeSend: animateSpinner(that, true),
success: function(data){
$(that.children()[0]).html(data.html);
2016-09-15 17:45:51 +08:00
commentFormOnSubmitAction();
2016-09-15 14:46:14 +08:00
animateSpinner(that, false);
},
complete: animateSpinner(that, false)
});
});
}
function refreshComments(child){
2016-09-15 17:45:51 +08:00
var parent = child.closest(".step-comment");
2016-09-15 14:46:14 +08:00
var link = parent.attr("data-href");
$.ajax({
method: 'GET',
url: link,
beforeSend: animateSpinner(parent, true),
success: function(data){
2016-09-15 17:45:51 +08:00
updateCommentHTML(parent, data);
2016-09-15 14:46:14 +08:00
animateSpinner(parent, false);
},
complete: animateSpinner(parent, false)
});
}
function commentFormOnSubmitAction(){
$(".comment-form")
.each(function() {
2016-09-15 17:45:51 +08:00
bindCommentAjax($(this));
});
}
function bindCommentAjax(form){
form
.ajaxSuccess( function(){
refreshComments(form);
2016-09-15 14:46:14 +08:00
});
}
2016-09-15 17:45:51 +08:00
function updateCommentHTML(parent, data) {
var comment_form = $(parent.find(".comment-form"));
$(parent.children()[0]).html(data.html);
bindCommentAjax(comment_form);
}
2016-09-15 14:46:14 +08:00
initializeComments();
})();