Z
Z
zggb2015-02-06 20:05:19
JavaScript
zggb, 2015-02-06 20:05:19

How to combine Masonry + ajax + infinitescroll?

I am using Masonry to display user cards. The problem is that when I use user filter with AJAX, js masonry doesn't fire, cards are output in 1 column. As I understand it, there is a conflict with the escape_javascript render. Tried disabling turbolinks, didn't help.

var $container = $('#masonry-container');
$container.empty();
$container.append("<%= escape_javascript render @users %>");


Here is the code to render in index.html.erb
<div class="col-md-9">
  <div id="masonry-container" class="transitions-enabled infinite-scroll clearfix centered content">
    <div id="filter">
      <%= render @users %>
    </div>
  </div>
  <div class="span7 text-center"><%= will_paginate %></div>
</div>


Masonry users.js code
$(document).ready(function(){
          var $container = $('#masonry-container');
      $container.imagesLoaded(function(){
          $container.masonry({
          itemSelector: '.box',
          columnWidth: function( containerWidth ) {
              return containerWidth / 20;
          },
          gutterWidth: 5,
          isAnimated: !Modernizr.csstransitions
          });
      });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zggb, 2015-02-07
@zggb

Yes, Masonry had to be called again.
Here's what I added and it worked.

$( document ).ajaxComplete(function() {
    $container.masonry('reloadItems').masonry();
});

V
Viktor Vsk, 2015-02-07
@viktorvsk

Where is the conflict?
Maybe you need to call the function after the elements are added again?
For example, in js.erb, like:

$container.masonry({
          itemSelector: '.box',
          columnWidth: function( containerWidth ) {
              return containerWidth / 20;
          },
          gutterWidth: 5,
          isAnimated: !Modernizr.csstransitions
          });

Or hang some kind of trigger. What is imagesLoaded()?
https://github.com/kristianmandrup/masonry-rails
stackoverflow.com/questions/24940041/infinite-scro...
railscasts.com/episodes/114-endless-page-revised?v...
These links are of no help ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question