在淘汰赛中重新应用绑定 [英] Reapply bindings in knockout
问题描述
我基本上想在同一页面上为不同的对象重新应用绑定,但是有奇怪的行为.重新应用绑定后,项目列表将丢失.
I basically want to reapply bindings on the same page for the different objects, but there is strange behavior. After I reapply the binding, the list of items is lost.
请在此处查看: http://jsfiddle.net/baio/9UcUs/5/
该怎么办?
推荐答案
简短的答案是不支持它.答案很长,那就是有一些解决方法.一种方法是在applyBindings
之前调用cleanNode
,但这不会清除事件处理程序.另一种方法是将视图模型包装为可观察的,然后更新该可观察的以重新应用绑定.效果更好,但仍然存在一些问题(请参见下文).
The short answer is that it's not supported. The long answer is that there are some ways around it. One way is to call cleanNode
before applyBindings
, but this doesn't clear event handlers. Another way is to wrap your view model in an observable and then update that observable to reapply bindings; this works much better, but still has a slight problem (see below).
这是您使用可观察视图模型方法的示例: http://jsfiddle.net/mbest/9UcUs/9/
Here is your example using the observable view model method: http://jsfiddle.net/mbest/9UcUs/9/
我发现使用可观察的视图模型的唯一问题是事件处理程序没有使用新的视图模型进行完全更新.他们将在新的视图模型中调用正确的函数,但是this
和data
值将用于原始视图模型.
The only problem I've found with using an observable view model is that event handlers are not completely updated with the new view model. They will call the correct function in the new view model, but the this
and data
values will be for the original view model.
淘汰赛3.0(目前计划于本月发布)完全支持可观察的视图模型.自定义绑定仍然可能存在问题,但希望所有这些都将很快被记录在案.
Knockout 3.0 (currently scheduled for release this month) fully supports observable view models. There still could be issues with custom bindings, but hopefully all this will be documented soon.
这篇关于在淘汰赛中重新应用绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!