暂停 knockout.js 1.2.1 中的绑定

发布于 2025-01-08 07:04:41 字数 235 浏览 1 评论 0原文

淘汰赛中有暂停和恢复绑定的选项吗?

版本: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨轻弹 2025-01-15 07:04:41

我认为没有办法在 Knockout.js 中暂停绑定。如果没有看到代码,很难说,但速度缓慢可能是由于您通过清除 observableArrays 并逐一添加新项目来刷新它们造成的。相反,您可以立即刷新整个数组:

...
self.manyItems = ko.observableArray();
...
function refreshItems(newItems){
    self.manyItems(newItems);
}

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:

...
self.manyItems = ko.observableArray();
...
function refreshItems(newItems){
    self.manyItems(newItems);
}
叹沉浮 2025-01-15 07:04:41

暂停和恢复是可能的:看看 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.

永不分离 2025-01-15 07:04:41

如果您确实仍然需要暂停订阅,我找到了一种方法,可以使用暂停计算在计算可观察量中暂停它们(想法取自 此网站)。我对其进行了一些修改,添加了pauseableCompulated 具有读写功能的能力。

viewModel.myComputedObservable = ko.pauseableComputed(function() {
    return myResult;
}, viewModel);

对于暂停,您可以调用 myCompulatedObservable.pause();,进行所有修改,然后调用 myCompulatedObservable.resume(); 进行这些修改,以触发计算的可观察量中的订阅。

   //wrapper for a computed observable that can pause its subscriptions
        ko.pauseableComputed = function (evaluatorFunction, evaluatorFunctionTarget) {
            var _cachedValue = "";
            var _isPaused = ko.observable(false);

            //the computed observable that we will return
            var result;
            if (evaluatorFunction.read) {
                result = ko.computed({
                    read: function() {
                        if (!_isPaused()) {
                            //call the actual function that was passed in
                            return evaluatorFunction.read.call(evaluatorFunctionTarget);
                        }
                        return _cachedValue;
                    },
                    write: function(value) {
                        if (!_isPaused()) {
                            //call the actual function that was passed in
                            return evaluatorFunction.write.call(evaluatorFunctionTarget, value);
                        }
                        return _cachedValue;
                    }
                }, evaluatorFunctionTarget);
            } else {
                result = ko.computed(function() {
                    if (!_isPaused()) {
                        //call the actual function that was passed in
                        return evaluatorFunction.call(evaluatorFunctionTarget);
                    }
                    return _cachedValue;
                }, evaluatorFunctionTarget);
            }

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.

viewModel.myComputedObservable = ko.pauseableComputed(function() {
    return myResult;
}, viewModel);

For pausing, you call myComputedObservable.pause();, make all your modifications and then call myComputedObservable.resume(); for those modifications to trigger the subscriptions in the computed observable.

   //wrapper for a computed observable that can pause its subscriptions
        ko.pauseableComputed = function (evaluatorFunction, evaluatorFunctionTarget) {
            var _cachedValue = "";
            var _isPaused = ko.observable(false);

            //the computed observable that we will return
            var result;
            if (evaluatorFunction.read) {
                result = ko.computed({
                    read: function() {
                        if (!_isPaused()) {
                            //call the actual function that was passed in
                            return evaluatorFunction.read.call(evaluatorFunctionTarget);
                        }
                        return _cachedValue;
                    },
                    write: function(value) {
                        if (!_isPaused()) {
                            //call the actual function that was passed in
                            return evaluatorFunction.write.call(evaluatorFunctionTarget, value);
                        }
                        return _cachedValue;
                    }
                }, evaluatorFunctionTarget);
            } else {
                result = ko.computed(function() {
                    if (!_isPaused()) {
                        //call the actual function that was passed in
                        return evaluatorFunction.call(evaluatorFunctionTarget);
                    }
                    return _cachedValue;
                }, evaluatorFunctionTarget);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文