One the most amazing things about Discourse is that it is a Single Page Application. The page is loaded once and the content is swapped on demand - This is my understanding.
I was trying to integrate Masonry with Discourse and ran into a small problem.
I got Masonry to load great on first page load and everything works great:
However, if I navigate to another topic-list page or select a category then it does not work anymore and I get this:
The method I use to call Masonry is to add the script below to the </head>
<script>
$(".topic-list").imagesLoaded(function() {
    $(".topic-list").masonry({
      itemSelector: ".topic-list-item"
    });
  });
</script>
I understand why the issue occurs - I think. It occurs because the script is only loaded and fired on initial page load.
This means that the <head> element is not reloaded on every page load.
So, logically, my next step was to see if I can add the script somewhere where it will get run again when new content loads.
So I tried
- </body>
- <footer>
Without luck. For some reason the script never fires again after initial page load.
So, my question is how can I make sure this script runs on every page load? (for now… selective logic will come in later)
<script>
$(".topic-list").imagesLoaded(function() {
    $(".topic-list").masonry({
      itemSelector: ".topic-list-item"
    });
  });
</script>

 ) but I’m now glad I found an official example to lean back on.
) but I’m now glad I found an official example to lean back on. That’s the one!
 That’s the one!








 , this will still log stuff to console but still very finicky about everything else and will only work with a jQuery function if the page is the first the user views and not on in-app navigation this is either a bug or something is running out of order. I don’t see any errors in the log though
, this will still log stuff to console but still very finicky about everything else and will only work with a jQuery function if the page is the first the user views and not on in-app navigation this is either a bug or something is running out of order. I don’t see any errors in the log though
 So I’m adapting it going forward.
 So I’m adapting it going forward.
