【问题标题】:rails 4 infinite scroll breaks javascript ajaxrails 4无限滚动中断javascript ajax
【发布时间】:2014-10-13 21:46:10
【问题描述】:

我在帖子索引页面中有 ajax cmets。我决定实现无限滚动,它工作正常,但是当我向下滚动并加载更多帖子(触发无限滚动)时,我在该页面上的 ajax cmets 不起作用。我尝试将 cmets js.coffee 添加到 posts.js.coffee 但在触发无限滚动后,cmets 显示为两倍。我想我需要在无限滚动之后调用我的 cmets 函数,但不知道我会在哪里再次实现它。

cmets.js.coffee

# comments.js.coffee
 jQuery ->
  # Create a comment
  $(".commentform")
    .on "ajax:beforeSend", (evt, xhr, settings) ->
      $(this).find('textarea')
        .addClass('uneditable-input')
        .attr('disabled', 'disabled');
    .on "ajax:success", (evt, data, status, xhr) ->
      $(this).find('textarea')
        .removeClass('uneditable-input')
        .removeAttr('disabled', 'disabled')
        .val('');
        $(xhr.responseText).hide().insertAfter($(this)).show('slow')




      $(document)
    .on "ajax:beforeSend", ".comment", ->
      $(this).fadeTo('fast', 0.5)
    .on "ajax:success", ".comment", ->
      $(this).hide('fast')
    .on "ajax:error", ".comment", ->
      $(this).fadeTo('fast', 1)

posts.js.coffee

  $(document).on 'ready, page:change', ->
  loading_posts = false
  $('a.load-more-posts').on 'inview', (e, visible) ->
    return if loading_posts or not visible
    loading_posts = true

    $.getScript $(this).attr('href'), ->
      loading_posts = false

      $("abbr.timeago").timeago();

index.js.haml

    $('#indexstream').append('#{escape_javascript(render partial: 'post', :locals => { :post => @posts }, :collection => @posts)}');
$('a.load-more-posts').attr('href', '#{posts_path page: @posts.next_page}');
- unless @posts.next_page
  $('a.load-more-posts').remove();

感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails ajax infinite-scroll


    【解决方案1】:

    我委派了这些活动,而且效果很好。

    更新代码:

     # comments.js.coffee
    jQuery ->
      # Create a comment
      $("body")
        .on "ajax:beforeSend", ".commentform", (evt, xhr, settings) ->
          $(this).find('textarea')
            .addClass('uneditable-input')
            .attr('disabled', 'disabled');
        .on "ajax:success", ".commentform", (evt, data, status, xhr) ->
          $(this).find('textarea')
            .removeClass('uneditable-input')
            .removeAttr('disabled', 'disabled')
            .val('');
          $(xhr.responseText).hide().insertAfter($(this)).show('slow')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 1970-01-01
      • 2012-06-13
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多