I am completely new to Symfony2 and don't understand fully routing and templating. Please help with following issue: I need to implement infinite scroll with Symfony 2 and Ajax. I have indexController that gets 10 gallery items from db. I want to get 10 more items every time I scroll down. Here is controller:
/**
* @Route("/", name="gallery_homepage")
*/
public function indexAction(Request $request)
{
$em = $this->getDoctrine()
->getEntityManager();
if($request->isXmlHttpRequest())
{
$page = $request->get('page', 1);
$limit = 10;
$start = $page * $limit - $limit;
$objects = $em->createQueryBuilder()
->select('o')
->from('ObjectBundle:Object', 'o')
->getQuery()
->setFirstResult($start)
->setMaxResults($limit)
->getResult();
$response = json_decode($objects);
return new Response(???));
}
else
{
}
}
This is twig template
{% block body %}
<section class="container">
<ul class="printedall">
{% for object in objects %}
<li class="printedimgs">
<a class="object_prints" href="object.url">
<img src="object.imageURL"/>
</a>
</li>
{% endfor %}
</ul>
</section>
{% endblock %}
What response must controller return to get 10 items from db and append them to displayed items?
This is javascript
is_processing = false;
last_page = false;
function addMoreElements() {
is_processing = true;
$.ajax({
type: "GET",
//FOS Routing
url: Routing.generate('gallery_homepage', {page: page}),
success: function(data) {
if (data.html.length > 0) {
$('.printedall').append(data.html);
page = page + 1;
last_page = data.last_page;
} else {
last_page = true;
}
is_processing = false;
},
error: function(data) {
is_processing = false;
}
});
}
$(window).scroll(function() {
var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
var scrolltrigger = 0.80;
if ((wintop / (docheight - winheight)) > scrolltrigger) {
if (last_page === false && is_processing === false) {
addMoreElements();
}
}
});