My working code without knockout binding is :
<div class="slick_demo_1">
<div>
<div class="ibox-content">
<h2>Slide 1</h2>
</div>
</div>
<div>
<div class="ibox-content">
<h2>Slide 2</h2>
</div>
</div>
<div>
<div class="ibox-content">
<h2>Slide 3</h2>
</div>
</div>
</div>
And to initiallize slick my js code is:
$('.slick_demo_1').slick({
dots: true
});
The code above is working fine. Now the code with knockout binding is:
<div class="slick_demo_1" data-bind="foreach:showSlider">
<div>
<div class="ibox-content">
<h2 data-bind="slider"></h2>
</div>
</div>
</div>
And to bind slick with DOM my code is:
ko.bindingHandlers.slick = {
init: function (element, valueAccessor) {
$(element).slick({
dots: true
});
}
};
But the custom binding is not working. What I am doing wrong?
The code you're showing hardly resembles the correct implementation of a custom binder.
Several hints:
destroyorunslick) if the element is disposed of by koslickOptionsbinding to pass additional objects to the slick initializationThe basic structure of a custom binding is explained here. See the code template:
};
As explained, the
initis responsible of initializing the caroulse, and theupdateis responsible of updating the content of the carousel when the array changes. R P Niemeyer makes an in depth explanation of custom binding here: Another Look at Custom Bindings for KnockoutJS.If you implement it correctly, your HTML code should look something like this:
On the
init, your custom binding should create the elements based on theimagesArray, and call the slick initialization, using theadditionalOptionsif present, and register the slick destruction when the element is disposed of by ko. In the update you should modify the inner elements and possible reapply slick. You should also review slick's API.This fiddle shows a partial implementation, but more complete that the current the floowing snippet:
The missing part in the implementation is the updating of the gallery if the imgUrls changes. But it shows the main techniques in the hints.