如何强制视图刷新而不从可观察对象自动触发它?
注意:这主要是为了调试和理解 KnockoutJS。
有没有办法显式请求 Knockout 从(已绑定)视图模型刷新视图?我正在寻找类似的东西:
ko.refreshView();
我知道这不是 Knockout 的预期用途,但我仍然想知道是否有这样的方法用于调试和学习目的。
Note: this is mostly for debugging and understanding KnockoutJS.
Is there a way to explicitly request Knockout to refresh the view from (already bound) view model? I am looking for something like:
ko.refreshView();
I understand that this is not an intended use of Knockout, but I still want to know if there is a such method for debugging and learning purposes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法在整个 viewModel 上调用某些内容,但在单个可观察对象上,您可以调用 myObservable.valueHasMutated() 来通知订阅者他们应该重新评估。正如您提到的,这在 KO 中通常是不必要的。
You can't call something on the entire viewModel, but on an individual observable you can call
myObservable.valueHasMutated()
to notify subscribers that they should re-evaluate. This is generally not necessary in KO, as you mentioned.在某些情况下,简单地删除绑定然后重新应用可能会很有用:
In some circumstances it might be useful to simply remove the bindings and then re-apply:
我在这里用我的bindHTML淘汰绑定处理程序创建了一个JSFiddle:
https://jsfiddle.net/glaivier/9859uq8t/
首先,将绑定处理程序保存到其自己的(或通用)文件并包含在淘汰赛之后。
如果您使用此功能,请将您的绑定切换到此:
I have created a JSFiddle with my bindHTML knockout binding handler here:
https://jsfiddle.net/glaivier/9859uq8t/
First, save the binding handler into its own (or a common) file and include after Knockout.
If you use this switch your bindings to this:
一个简单的替代方法是清除 observable 数组:
注意:这种替代方法可能会影响性能。
An easy alternative is to clear the observable array:
Note: This alternative could perhaps suffer in performance.