暂停 knockout.js 1.2.1 中的绑定
淘汰赛中有暂停和恢复绑定的选项吗?
版本:knockout.js 1.2.1
我们对暂停绑定的需求源于以下原因。在某些操作中,我们必须从服务器加载大量数据,例如,多个选择的整个数据都会更改,有些表的行会动态添加等。
现在,在当前场景中,表单与视图模型完全绑定。当我们清除组合并添加每个项目时,视图会刷新,因此存在明显的延迟。如果我有办法暂停绑定,我可以暂停,然后将所有数据加载到视图模型中,然后再次恢复绑定。
Is there any option to suspend and resume bindings in knockout?
Version : knockout.js 1.2.1
Our need for suspending bindings stems from the following. During some operations we have to load a lot of data from the server, eg multiple selects have their entire data changed, there are tables whose rows are dynamically added etc.
Now in this current scenario, the form is fully bound with the view model. When we clear the combos and add each item, the view gets refreshed hence there is significant delay. If I had the means to suspend binding, I could suspend, then load all data into the viewmodel and then resume binding again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为没有办法在 Knockout.js 中暂停绑定。如果没有看到代码,很难说,但速度缓慢可能是由于您通过清除 observableArrays 并逐一添加新项目来刷新它们造成的。相反,您可以立即刷新整个数组:
I don't think there is a way to suspend binding in knockout.js. Without seeing the code it's hard to say, but the slowness is probably caused by the fact that you refresh you observableArrays by clearing them and adding new items one by one. Instead you can refresh the entire array at once:
暂停和恢复是可能的:看看 Ryan Niemeyer 制作的这个演示。请参阅他的博客上的此条目了解更多背景信息。
Suspending and resuming is possible: take a look at this demo put together by Ryan Niemeyer. Refer to this entry on his blog for more background information.
如果您确实仍然需要暂停订阅,我找到了一种方法,可以使用暂停计算在计算可观察量中暂停它们(想法取自 此网站)。我对其进行了一些修改,添加了pauseableCompulated 具有读写功能的能力。
对于暂停,您可以调用
myCompulatedObservable.pause();
,进行所有修改,然后调用myCompulatedObservable.resume();
进行这些修改,以触发计算的可观察量中的订阅。If you really need to pause subscriptions still, I found a way to pause them in computed observables with a pauseableComputed (idea taken from this site). I modified it a bit to add the pauseableComputed the ability to have read and write functions.
For pausing, you call
myComputedObservable.pause();
, make all your modifications and then callmyComputedObservable.resume();
for those modifications to trigger the subscriptions in the computed observable.