1
我正在处理图像列表。图像被动态加载;引用列表存储在observableArray中。 完成图像列表的加载后,我想连接DOM元素的处理程序。我此刻的实现:实现基于afterRender功能的敲除自定义绑定
在查看:
<div class="carousel_container" data-bind="template: { 'name': 'photoTemplate', 'foreach': ImageInfos, 'afterRender': renderCarousel }">
<script type="text/html" id="photoTemplate">
//...content of template
</script>
在视图模型
:
self.counterCarousel = 0;
self.renderCarousel = function (elements) {
var allImagesCount = self.ImageInfos().length;
self.counterCarousel++;
if (self.counterCarousel >= allImagesCount) {
self.counterCarousel = 0;
// ... add handlers here
}
}
这是一个非常丑陋的做法。另外,用户可以添加/删除图像,因此每次添加或删除后都需要删除所有处理程序并重新连接。我如何组织自定义绑定来处理这种情况?