Javascript 用于光滑js的敲除自定义绑定不起作用
我的无敲除绑定的工作代码是:Javascript 用于光滑js的敲除自定义绑定不起作用,javascript,knockout.js,slick.js,ko-custom-binding,Javascript,Knockout.js,Slick.js,Ko Custom Binding,我的无敲除绑定的工作代码是: <div class="slick_demo_1"> <div> <div class="ibox-content"> <h2>Slide 1</h2> </div>
<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>
上面的代码运行良好。
现在,具有敲除绑定的代码是:
<div class="slick_demo_1" data-bind="foreach:showSlider">
<div>
<div class="ibox-content">
<h2 data-bind="slider"></h2>
</div>
</div>
</div>
但是自定义绑定不起作用。我做错了什么?您显示的代码与自定义活页夹的正确实现几乎不相似 几点提示:
- 您应该在根元素中使用自定义绑定
- 定制绑定应负责构建,如果可能的话,从元素中移除旋转木马API(使用slick的
或destroy
),前提是元素由ko处理unslick
- 自定义活页夹应能够更新旋转木马内的元素
- 使用额外的
绑定将额外的对象传递给slick初始化将是一件有趣的事情slickOptions
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
负责初始化转盘,而update
负责在阵列改变时更新转盘的内容。R P Niemeyer在此处对自定义绑定进行了深入解释:
如果正确实现,您的HTML代码应该如下所示:
<div data-bind="slick: imagesArray, slickOptions: additionalOptions>
</div>
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.
}
<div data-bind="slick: imagesArray, slickOptions: additionalOptions>
</div>