2011-06-18 57 views
1

我有一個有四個輸入字段的表單。當用戶界面更新時,我需要更新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 

}); 

但我要如何更新方法裏面的人?對我來說,它看起來像是堆棧溢出情況...

+0

其他字段是否使用相同的值更新? – Dan

回答

3

在Knockout 1.2.1中,如果observable使用完全相同的值進行更新,則不會發送更新。所以,在這種情況下,你應該沒問題,如果每個值都用相同的值更新。

以前,避免遞歸循環更新的最好方法是檢查您的訂閱是否需要更新observable。這將削減更新鏈。

所以:

myViewModel.one.subscribe(function(newValue) { 

    if (two() !== "the right value") { 
     two("the right value"); 
    } 
    //etc... 
}); 

否則,如果還有更多的情況下,則有可能是可以探討的寫dependentObservables其他選項。不過,如果您獲得的不僅僅是雙向關係,手動訂閱通常是最好的選擇。