Я использую «infinite scroll plugin», чтобы добавить контент динамически. В каждом посте у меня есть Как и кнопка, но она не работает на содержание добавленного с помощью "бесконечной прокрутки"JQuery .on для содержимого, добавленного в будущем
HTML (ERB)
<% current_user.likes?(post) ? like_icon_name = "icon-thumbs-up" : like_icon_name = "icon-thumbs-up-alt" %>
<%=link_to "", url_for(controller: "like", action:"toggle", resource_name: post.class.to_s, resource_id: post.id), class:"like #{like_icon_name}", remote: true%>
HTML (генерируемой)
<a class="like icon-thumbs-up-alt" data-remote="true" href="/it/like/toggle?resource_id=1&resource_name=Photo"></a>
like.js
$(document).on("ready page:load", function() {
$("a[data-remote].like").on("ajax:success", function(e, data, status, xhr){
if(xhr.responseText){
$(e.target).toggleClass("icon-thumbs-up-alt icon-thumbs-up");
}else{
console.log(xhr);
}
});
});
Scrolling.js
// usage:
// $(elem).infinitescroll(options,[callback]);
// infinitescroll() is called on the element that surrounds
// the items you will be loading more of
$(document).on('ready page:load', function() {
$('#posts').infinitescroll({
navSelector : "nav.pagination",
// selector for the paged navigation (it will be hidden)
nextSelector : "nav.pagination .next a:first",
// selector for the NEXT link (to page 2)
itemSelector : "#posts div.post",
// selector for all items you'll retrieve
debug : false,
// enable debug messaging (to console.log)
}, function(arrayOfNewElems){
$(document).trigger('scrolled');
// optional callback when new content is successfully loaded in.
// keyword `this` will refer to the new DOM content that was just added.
// as of 1.5, `this` matches the element you called the plugin on (e.g. #content)
// all the new elements that were found are passed in as an array
});
});
страница: нагрузка на "turbolinks"
Не могли бы вы добавить код о храповой случае, что, как Buton? – CronosS
сделано. Вопрос обновлен – sparkle
Thx, попробуйте отредактировать like.js с помощью: '$ (document) .on (" ajax: success "," a data-remote] .like ", function (....' – CronosS