[英]Knockout subscribe event fired twice
我正在使用KO可观察数组,并且我试图在其自己的.subscribe方法中更改其可观察字段之一,这将引发两次相同的事件。 我如何避免这种情况? 下面的代码
$.each(self.PricingList(), function (i, pricing) {
var previousValue = pricing.Currency().code();
pricing.Currency().code.subscribe(function () {
ShowConfirmation(null, 'Are you sure ?',
function () {
// will be executed if user click on Yes
},function () {
// Will be executed if user clicks No
// How can i restore previous value ?
pricing.Currency().code(previousValue);
// the moment i change currency code's value its showing me confirmation dialog again, which i want to avoid.
});
});
$.each(self.PricingList(), function (i, pricing) {
pricing.Currency().code.subscribe(function (previousValue) {
ShowConfirmation(null, 'Are you sure ?',
function () {
},function () {
pricing.Currency().code(previousValue);// this line calls subscribe method again because the value in this observable changes, so try to remove this line from here.
});
}, null, "beforeChange");
这不是直接回答您的问题,但我相信,如果我正确假设,它将解决根本的问题:您正在尝试进行可选的不可更改的更改。
如果您看这篇博客文章,它描述了一种方法,其中在观察者周围使用包装器(类似于扩展程序),该包装器提供了beginEdit
和cancelEdit
函数,可以根据需要调用它们。
此源代码可在此Github存储库中找到 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.