我没有敲除绑定的工作代码是:
<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>
初始化光滑我的js代码是:
$('.slick_demo_1').slick({
dots: true
});
上面的代码工作正常。 现在敲除绑定的代码是:
<div class="slick_demo_1" data-bind="foreach:showSlider">
<div>
<div class="ibox-content">
<h2 data-bind="slider"></h2>
</div>
</div>
</div>
为了用DOM绑定光滑,我的代码是:
ko.bindingHandlers.slick = {
init: function (element, valueAccessor) {
$(element).slick({
dots: true
});
}
};
但是自定义绑定无效。我做错了什么?
答案 0 :(得分:4)
您展示的代码几乎不像自定义活页夹的正确实现。
一些提示:
destroy
或unslick
)如果元素被ko处理slickOptions
绑定将其他对象传递给光滑初始化会很有趣自定义绑定is explained here的基本结构。请参阅代码模板:
ko.bindingHandlers.yourBindingName = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// This will be called when the binding is first applied to an element
// Set up any initial state, event handlers, etc. here
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// This will be called once when the binding is first applied to an element,
// and again whenever any observables/computeds that are accessed change
// Update the DOM element based on the supplied values here.
}
};
如上所述,init
负责初始化caroulse,update
负责在阵列更改时更新轮播的内容。 R P Niemeyer在这里对自定义绑定进行了深入的解释:Another Look at Custom Bindings for KnockoutJS。
如果您正确实现它,您的HTML代码应如下所示:
<div data-bind="slick: imagesArray, slickOptions: additionalOptions>
</div>
在init
上,您的自定义绑定应根据imagesArray
创建元素,并使用additionalOptions
(如果存在)调用光滑初始化,并在元素由ko处理。在更新中,您应该修改内部元素并可能重新应用光滑。您还应该查看slick's API。
This fiddle显示了部分实现,但更完整的是当前的floowing片段:
var imageUrls = [];
var i = 1;
for (i=1;i<=10;i++)
imageUrls.push('http://lorempixel.com/400/200/animals/'+i);
ko.bindingHandlers.slick = {
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) {
$div = $('<div>');
$image = $('<img>');
$image.attr('src',imgUrl);
$div.append($image);
$(element).append($div)
});
}
// 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');
});
},
//update the control when the view model changes
update: function(element, valueAccessor) {
var images = ko.unwrap(valueAccessor());
// Do something to update the content
}
};
var vm = {
images: ko.observableArray(imageUrls),
options: {}
}
ko.applyBindings(vm, gallery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.slick/1.5.8/slick.min.js"></script>
<link href="https://cdn.jsdelivr.net/jquery.slick/1.5.8/slick.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/jquery.slick/1.5.8/slick-theme.css" rel="stylesheet">
<div id="gallery"
style="width:400px"
data-bind="slick: images, slickOptions: {dots:true, initialSlide:4}">
</div>
如果imgUrls发生变化,实现中缺少的部分就是更新库。但它显示了提示中的主要技巧。