我如何取消对带有Knockout 3.0的可观察数组的更改? [英] How might I cancel a change to an observable array w/ Knockout 3.0?

查看:70
本文介绍了我如何取消对带有Knockout 3.0的可观察数组的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对数组进行了更改.我正在使用Sanderson的最新数组订阅方法来捕获添加/删除更改.在此订阅中,我打算捆绑并通过有线发送我的请求.如果请求由于任何原因失败,我希望能够取消对该集合的任何可能更改.我已经确认此订阅在更改传播之前已被击中,所以我认为会有一种方式说停止不要做",但是我不知道怎么做.

I have a change occurring to an array. I am using Sanderson's latest array subscription method to catch the add/delete change. In this subscription is where I intend to bundle and send my request over the wire. If the request fails for any reason I want to be able to cancel any possible changes to the collection. I have verified that this subscription is hit before the change propagates so I assume there would be a way to say "STOP DON'T DO IT" however I can't figure out how.

作为我的例子...

self.SourceData = ko.observableArray(data);
self.SourceData.subscribe(function(changes) {
    var isAllGood = true;
    ko.utils.arrayForEach(changes, function(ch) {
        if (ch.value == doesNotMeetMyCondition) isAllGood = false;
    });
    if (!isAllGood) <STOP DON'T DO IT>
}, null, 'arrayChange');

在检查'this'时,我确实看到了带有标准[callback,dispose,disposeCallback,target]的ko.subscription对象,但是似乎没有什么可以做的就是STOP Do n't Do It.

When inspecting 'this' I do see the ko.subscription object with the standard [callback, dispose, disposeCallback, target] but nothing seems to amount to STOP DON'T DO IT.

任何想法都会很有帮助.谢谢.

Any thoughts would be quite helpful. Thanks.

推荐答案

当前没有一种阻止更改进行的方法.

There is not currently a way to stop the change from going through.

您可能可以预订beforeChange事件,并在更改之前缓存阵列的副本.像这样:

You could potentially subscribe to the beforeChange event and cache a copy of the array before the change. Something like:

self.SourceData.subscribe(function(value) {
    /store copy, in case it needs to be restored
    self.beforeSourceData = value && value.slice();
}, null, "beforeChange");

这篇关于我如何取消对带有Knockout 3.0的可观察数组的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆