var imageUrls = [];
var i = 1;
for (i=1;i<=10;i++)
imageUrls.push('http://lorempixel.com/400/200/animals/'+i);
ko.bindingHandlers.slick = (function() {
var createImageDiv = function(imgUrl) {
$div = $('<div>');
$image = $('<img>');
$image.attr('src',imgUrl);
$div.append($image);
return $div;
};
// Initializes the carousel
var init = function(element, valueAccessor, allBindingsAccessor) {
// Clears the div
$(element).empty();
// Creates the inner divs with images
var images = ko.unwrap(valueAccessor());
if (images) {
images.forEach(function(imgUrl) {
$(element).append(createImageDiv(imgUrl));
});
}
// try to recover slickOptions
var options = allBindingsAccessor().slickOptions || {};
// Initialize slick on the div, with provided options
$(element).slick(options);
//handle disposal, if KO removes the element
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).slick('unslick');
});
// check if there is slickIndex
var slickIndex = allBindingsAccessor().slickIndex;
if (slickIndex) {
$(element).slick('slickGoTo', ko.unwrap(slickIndex));
}
// If it's obervable, subscribe to its changes
if (ko.isObservable(slickIndex)) {
slickIndex.subscribe(function(idx) {
$(element).slick('slickGoTo', idx);
});
}
// It if's writable observable, update when slick current index changes
if (ko.isWritableObservable(slickIndex)) {
$(element).on('afterChange', function(evt, slick, pos) {
slickIndex(pos);
})
}
// Check if the array of images is an observable array
var imagesArray = valueAccessor();
// If it's observable array, subscribe to...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.