I have a Rails 5.1.3 application (a Facebook clone to be more precise.)
When the page initially loads, I fire an anonymous function to grab all of the "comment" and "reply" buttons. These buttons are responsible for revealing the comment/reply forms.
The problem I'm having is this, when I use pagination to grab additional posts with ajax, I fire the exact same functions again, this time named updatebuttons(), however, sometimes it works, sometimes it doesn't.
Here's my application.js. which loads the initial buttons on $(document).ready(function(( {...})
$(function() {
if ($(".search")) {
tabs = $("li.tab").toArray();
tabs.forEach(function(item) {
item.addEventListener("click", function(e) {
tab = $($(e.target)).parents("li").attr("data-panel");
$("ul#list li").not(".hidden").toggleClass("hidden");
$("." + tab).toggleClass("hidden");
})
})
}
// initial grab of comment buttons for revealing comment forms on posts
$("div.post").on("click", ".js-comment-button", (e) => {
console.log("grabbed comment buttons")
e.preventDefault();
form = $(e.target).parents(".post").children(".js-post-comment-form").removeClass("hidden").addClass("active");
form.find("input.input").focus();
input = form.find("input.input");
input.on("focusout", function() {
form.addClass("hidden").removeClass("active");
})
})
// initial grab of reply buttons for comments
$("div.comment-body").on("click", "a[id*='js-reply-comment']", (e) => {
console.log("grabbed reply buttons")
e.preventDefault()
$($(e.target)).parent(".comment-likes").siblings(".replies").toggleClass("hidden")
$($(e.target)).parent(".comment-likes").siblings(".js-reply-comment").toggleClass("hidden").find("input").focus();
})
// close document.ready
})
Here's my pagination code which fires when the user has gotten to the bottom of the page.
$(document).ready(function() {
$(window).on('scroll', function() {
const more_posts_url = $('.pagination span.next a[rel="next"]').attr('href');
if (more_posts_url && ($(window).scrollTop() > ($(document).height() - $(window).height() - 60))) {
$('.pagination').html('<img id="loading" src="/assets/ajax-loader.gif" alt="Loading..." title="Loading..." />');
// Updates posts with infinite pagination
$.getScript(more_posts_url)
.done(function(){
console.log("updating buttons!")
// comment form button
$("div.post").on("click", ".js-comment-button", (e) => {
console.log("comment button updated and clicked")
e.preventDefault();
form = $(e.target).parents(".post").children(".js-post-comment-form").removeClass("hidden").addClass("active");
form.find("input.input").focus();
input = form.find("input.input");
input.on("focusout", function() {
form.addClass("hidden").removeClass("active");
})
})
// reply to comment button
$("div.comment-body").on("click", "a[id*='js-reply-comment']", (e) => {
console.log("reply button updated and clicked")
e.preventDefault()
$($(e.target)).parent(".comment-likes").siblings(".replies").toggleClass("hidden");
$($(e.target)).parent(".comment-likes").siblings(".js-reply-comment").toggleClass("hidden").find("input").focus();
})
})
.fail(function() {
$("#loading").hide();
})
}
return;
// close #infinite scrolling
});
// close document ready
});
Additional documentation: here's a YouTube video demonstrating the behavior.
I know this may not be the best way to implement the desired behavior but I'm just trying to get it working before making it cleaner. I am also looking for a better implementation of grabbing new buttons (e.g. the ones updated via AJAX).
bundle showit lists Turbo links as a dependency, I guess I am then turbolinks (5.0.1) turbolinks-source (5.0.3)