0

我正在使用Masonry-rails的无限滚动选项,并正在通过Raty.js实现评级系统。当前几个 pin 出现时,会显示平均评分,但是,无限滚动后,新 pin 没有平均评分。附上我的 index.html.haml。如何允许 raty.js 显示无限滚动新引脚的平均评分?

这是我的临时站点的链接:http: //instapin.herokuapp.com/(向下滚动到底部)

index.html.haml:

#pins.transitions-enabled.infinite-scroll
  - @pins.each_with_index do |pin, i|
    .box.panel.panel-default
        = link_to (image_tag pin.image.url), pin
        .panel-body
            %h2= link_to pin.title, pin
            %p.user
            Submitted by
            = link_to pin.user.name, user_path("id" => pin.user.id)
        .panel-footer
            .btn-group.pull-left
                %div{class: "srating", dscore: @avg_reviews[i] }
            .btn-group.pull-right
                - if user_signed_in?
                    - if current_user.voted_for? pin
                        = link_to unlike_pin_path(pin), method: :put, class: "btn btn-default vote", data: { type: :json } do
                            %span.glyphicon.glyphicon-heart
                                =pin.get_upvotes.size
                    - else
                        = link_to like_pin_path(pin), method: :put, class: "btn btn-default vote", data: { type: :json } do
                            %span.glyphicon.glyphicon-heart
                                =pin.get_upvotes.size
                - else
                    = link_to like_pin_path(pin), method: :put, class: "btn btn-default" do
                        %span.glyphicon.glyphicon-heart
                            =pin.get_upvotes.size
  #page-nav.container
    = will_paginate @pins, renderer: BootstrapPagination::Rails, previous_label: "Newer", next_label: "Older", class: "paginate"

  :javascript
    $('.srating').raty({
        path: '/assets/',
        readOnly: true,
        score: function() {
            return $(this).attr('dscore');
        }
    });
    $(function(){

      var $container = $('#pins');

      $container.imagesLoaded(function(){
        $container.masonry({
          itemSelector: '.box',
          columnWidth: 10
        });
      });

      $container.infinitescroll({
        navSelector  : '#page-nav',    // selector for the paged navigation
        nextSelector : '#page-nav a',  // selector for the NEXT link (to page 2)
        itemSelector : '.box',     // selector for all items you'll retrieve
        loading: {
            finishedMsg: 'No more pins',
            img: 'http://i.imgur.com/6RMhx.gif'
          }
        },
        // trigger Masonry as a callback
        function( newElements ) {
          // hide new items while they are loading
          var $newElems = $( newElements ).css({ opacity: 0 });
          // ensure that images load before adding to masonry layout
          $newElems.imagesLoaded(function(){
            // show elems now they're ready
            $newElems.animate({ opacity: 1 });
            $container.masonry( 'appended', $newElems, true );
          });
        }
      );

    });
4

1 回答 1

1

我已经解决了问题所在。我需要添加 raty.js 以在无限滚动发生后加载。生成的 javascript 应该是:

:javascript
    $('.srating').raty({
        path: '/assets/',
        readOnly: true,
        score: function() {
            return $(this).attr('dscore');
        }
    });
    $(function(){

      var $container = $('#pins');

      $container.imagesLoaded(function(){
        $container.masonry({
          itemSelector: '.box',
          columnWidth: 10
        });
      });

      $container.infinitescroll({
        navSelector  : '#page-nav',    // selector for the paged navigation
        nextSelector : '#page-nav a',  // selector for the NEXT link (to page 2)
        itemSelector : '.box',     // selector for all items you'll retrieve
        loading: {
            finishedMsg: 'No more pins',
            img: 'http://i.imgur.com/6RMhx.gif'
          }
        },
        // trigger Masonry as a callback
        function( newElements ) {
          // hide new items while they are loading
          var $newElems = $( newElements ).css({ opacity: 0 });
          // ensure that images load before adding to masonry layout
          $newElems.imagesLoaded(function(){
            // show elems now they're ready
            $newElems.animate({ opacity: 1 });
            $container.masonry( 'appended', $newElems, true );
                    $('.srating').raty({
        path: '/assets/',
        readOnly: true,
        score: function() {
            return $(this).attr('dscore');
        }
    });
          $('.srating').raty('reload');

          });
        }
      );

    });
于 2015-02-23T20:41:52.160 回答