![](/img/trans.png)
[英]Knockout JS subscribe to 2 observables in a such a way that if either of the 2 change, I only get notified once
[英]knockout subscribe to observables needed?
我有一個包含四個輸入字段的表單。 從UI更新一個時,我需要更新res(3)。 意味着要更新的一個可以是四個中的任何一個。
myViewModel.four.subscribe(function(newValue) {
//update one here
//update two here
//update three here
});
等等.....
myViewModel.one.subscribe(function(newValue) {
//update two here
//update three here
//update four here
});
但是我應該如何更新方法中的那些呢? 對我來說,這看起來像是堆棧溢出的情況...
在淘汰賽1.2.1中,如果可觀察值的更新值完全相同,則不會發送更新。 因此,在這種情況下,如果每個值都更新為相同的值,則應該可以。
但是,以前,避免遞歸更新循環的最佳方法是簽入您的訂閱,是否確實需要更新可觀察對象。 這將減少更新鏈。
所以:
myViewModel.one.subscribe(function(newValue) {
if (two() !== "the right value") {
two("the right value");
}
//etc...
});
否則,如果您的方案還有更多問題,那么可能會有其他可寫的dependentObservables選項可供探索。 但是,當您獲得的不是雙向關系時,手動訂閱通常是最好的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.