如果依赖项位于错误的分支语句中,则计算将永远停止触发 [英] Computed stops triggering forever if dependency is inside of false branch statement
问题描述
我遇到了一个问题,即我的计算得到的可观察值在某些顺序的依存关系更改后停止触发.最终我发现了这一点:如果在最新评估期间依赖项位于错误的分支语句中,则即使条件在评估完成之前变为真,下次也不会触发计算. 这是一个示例: https://jsfiddle.net/sgs218w0/1/
I'm faced a problem that my computed observable stops triggering after some sequence of dependency changes. Finally I found out the point: if dependency was inside of false branch statement during latest evaluation, computed will not be triggered next time even if condition became true before evaluation finished. Here is a sample: https://jsfiddle.net/sgs218w0/1/
var viewModel = new function(){
var self = this;
self.trigger = ko.observable(true);
self.fire = function(){
self.trigger(! self.trigger());
};
self.content = function(){
var test = 3;
return ko.computed(function(){
alert("triggered!");
if(test !== 0){
console.log(self.trigger());
alert(test);
}
test--;
});
}();
};
ko.applyBindings(viewModel);
是错误还是功能?您知道此问题的任何解决方法吗?我似乎正在优化,但对我来说它看起来有些激进且不正确. (我改变了主意.这是合理的,但有时可能会导致一些问题.我认为淘汰赛应该有解决此问题的选项)
Is it bug or feature? Do you know any workaround for this issue? I seems to be optimization, but it looks aggressive and incorrect for me. ( I changed my mind. It is reasonable, but can lead to some issues sometimes. I think knockout should have options to fix this issues)
P.S.如果需要,我可以发布更详细的真实代码示例,以使问题更具体.但是实际代码的要点是一样的.
P.S. I could publish more detailed example of real code to make question more specific, if you need it. But the point of real code it the same.
更新 好吧,我不必太懒惰来提供我想要实现的目标的更详细的示例.我喜欢compute的想法,它可以自动进行ajax调用.在此处中描述.我看到的一个缺点是,即使UI的相应部分不可见,也会进行调用.我试图通过以下方式修复它: https://jsfiddle.net/bpr88bp3/1/.问题在于,一旦取消激活制表符,就无法再激活它,因为计算停止触发...
UPDATE Well, I had to be less lazy to provide more detailed example of what I want achieve. I like the idea of computed which automatically make ajax calls. Described here. One disadventure I see is that call will be made even if corresponding part of UI is invisible. I tried to fix it this way: https://jsfiddle.net/bpr88bp3/1/. The problem is that once tab is deativated it can't be activated anymore, because computed stops triggering...
推荐答案
在阅读完您的问题的更新并浏览了更新的示例代码后,我想出了一个真正的解决方案.这利用pureComputed
进行更新,这是因为可以通过订阅纯纯的计算并处置预订来激活和停用纯计算.这是重要的代码:
After reading the update to your question and looking through the updated example code, I've come up with a real solution. This uses a pureComputed
to do the update, taking advantage of the fact that a pure computed can be activated and deactivated by subscribing to it and disposing the subscription. Here is the important code:
updateComputed = ko.pureComputed(function () {
updateTrigger();
result(evaluator.call(owner));
});
ko.computed(function () {
var isActive = result.active();
if (isActive && !updateSubscription) {
updateSubscription = updateComputed.subscribe(function () {});
} else if (updateSubscription && !isActive) {
updateSubscription.dispose();
updateSubscription = undefined;
}
});
https://jsfiddle.net/mbest/bpr88bp3/2/
这篇关于如果依赖项位于错误的分支语句中,则计算将永远停止触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!