Skip to content

Commit

Permalink
Fast home
Browse files Browse the repository at this point in the history
  • Loading branch information
Swader committed Nov 22, 2017
1 parent 073b3a6 commit c69eee2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Controller/HomeController.php
Expand Up @@ -31,7 +31,7 @@ public function __construct(Twig_Environment $twig, EntityManager $em)
*/
public function homeAction()
{
$galleries = $this->em->getRepository(Gallery::class)->findBy([], ['createdAt' => 'DESC']);
$galleries = $this->em->getRepository(Gallery::class)->findBy([], ['createdAt' => 'DESC'], self::PER_PAGE);
$view = $this->twig->render('home.html.twig', [
'galleries' => $galleries,
]);
Expand Down
38 changes: 38 additions & 0 deletions templates/home.html.twig
Expand Up @@ -20,4 +20,42 @@

</div>

{% endblock %}


{% block javascripts %}
{{ parent() }}

<script>
$(function () {
var nextPage = 2;
var $galleriesContainer = $('.home__galleries-container');
var $lazyLoadCta = $('.home__lazy-load-cta');
function onScroll() {
var y = $(window).scrollTop() + $(window).outerHeight();
if (y >= $('body').innerHeight() - 100) {
$(window).off('scroll.lazy-load');
$lazyLoadCta.click();
}
}
$lazyLoadCta.on('click', function () {
var url = "{{ url('home.lazy-load') }}";
$.ajax({
url: url,
data: {page: nextPage},
success: function (data) {
if (data.success === true) {
$galleriesContainer.append(data.data);
nextPage++;
$(window).on('scroll.lazy-load', onScroll);
}
}
});
});
$(window).on('scroll.lazy-load', onScroll);
});
</script>
{% endblock %}

0 comments on commit c69eee2

Please sign in to comment.