I've been trying to solve this for a couple days now, but couldn't make it work. In replacing old flash brands-we-sell-like-banner at some e-commerce site we run, for a Javascript and mobile-friendly solution, I'm trying to catch up with this requirements:
- Get brand list and settings (i.e. max items per slide) thru AJAX (done).
- Use bootstrap carousel (done).
- Dynamic carousel content rendering, using Knockout
The issue is on the latter, as I can't find a way to draw contents per-slide. A non-dynamic rendering (straight html based), of what I'm trying to achieve, can be seen in this SO question (with jsfiddle companion).
I'm having troubles with the KO foreach binding, in order to render multiple-items per slide.
Some insights on the following code...
- brands: the brand structure List. An observableArray, containing non-observable structures representing brands (name, image-url, etc.)
- $parent.isFirst(): returns if a new slide must be drawn, given the max amount of items per slide
- $parent.defineItemClass: returns the class for the slide (it only marks the first one with 'active item', the rest are only 'item'
- $parent.drawTail: tells if the contained html must be drawn or not
The template...
<script type="text/html" id="brand-widget-template">
<div class="container-fluid">
<div class="row-fluid">
<div class="carousel slide" id="brands-carousel">
<div class="carousel-inner" data-bind="foreach: brands">
<!-- ko if: $parent.isFirst() -->
<div data-bind="css: $parent.defineItemClass($index)">
<ul class="thumbnails">
<!-- /ko -->
<li class="span1">
<div class="caption">
<h5 data-bind="text: Name"></h5>
</div>
<div class="thumbnail">
<img src="#" alt="">
</div>
</li>
<!-- ko if: $parent.drawTail() -->
</ul>
</div>
<!-- /ko -->
</div>
</div>
</div>
</div>
I must have a misunderstanding on the way the rendering gets done. In debugging, I notice that $parent.drawTail, never gets called. Any ideas?
Here's a fiddle on wich I isolated the most important pieces from the project.
Thanks! Luigi