applyBindings到部分视图

applyBindings到部分视图

本文介绍了KnockoutJS 2.3:“自定义绑定" +"ko.applyBindings到部分视图"会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用基因敲除.js,自定义绑定和对部分视图调用applyBindings()的代码. 使用ko 2.2.1的jsfiddle

I've got some code that uses knockout.js, a custom binding and a calls applyBindings() to a partial view. jsfiddle with ko 2.2.1

    var handle = slider.slider().find(".ui-slider-handle").first();
    $(handle).attr("data-bind", "tooltip: viewModel.value");
    ko.applyBindings(viewModel.value, $(handle)[0]);

现在,在ko版本2.3中,出现错误您不能多次将绑定应用于同一元素". 使用ko 2.3的jsfiddle

Now, with ko version 2.3, I get the error "You cannot apply bindings multiple times to the same element." jsfiddle with ko 2.3

我可能一直都有这个问题,但是以前版本的淘汰赛不会抛出异常.我已经在部分applyBindings之前添加了对cleanNode()的调用,但没帮助.

I've probably always had this problem but previous versions of knockout wouldn't throw an exception.I've added a call to cleanNode() just before the partial applyBindings but that doesn't help.

    var handle = slider.slider().find(".ui-slider-handle").first();
    $(handle).attr("data-bind", "tooltip: viewModel.value");
    ko.cleanNode($(handle)[0]);
    ko.applyBindings(viewModel.value, $(handle)[0]);

有趣的是,绑定仅适用于动态工具提示,而不适用于静态字段,因此我认为在应用部分视图绑定之后会引发错误.我试图遵循在全局ko.applyBindings()之后发生的调用链,但是它嵌套得很深,并且迷路了.我所知道的是,自定义绑定将在ko.applyBindings()之后初始化.我不知道以后是否有添加自定义处理程序的方法,也许有帮助.我希望这很清楚.

Interestingly, binding works with the dynamic tooltip but not with a static field, so I think the error is thrown after applying the partial view binding.I've tried to follow the chain of calls that occurs after the global ko.applyBindings() but it's deeply nested, and got lost.All I know is that the custom binding is initialized then, after ko.applyBindings(). I don't know whether there's a way to add custom handlers later on, perhaps that could help.I hope that's clear enough.

链接的问题.

推荐答案

当然! 我什至不需要在局部视图中调用applyBindings(),并添加了正确的属性足够:

Of course!! I don't even need to call applyBindings() to the partial view, adding the right attribute is enough:

var handle = slider.slider().find(".ui-slider-handle").first();
$(handle).attr("data-bind", "tooltip: viewModel.value");
/*ko.applyBindings(viewModel.value, $(handle)[0]);*/

这篇关于KnockoutJS 2.3:“自定义绑定" +"ko.applyBindings到部分视图"会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 19:42