简体   繁体   中英

How to find the sender or source which causes the ko.computable to trigger

I am working on Typescript and KnockoutJs. I am trying to get the sender for knockout computable function. Basically i have two controls, if either of the control's value gets changed then my knockout computable will be called,

self.userValue = ko.computed(() => { 
if(self.control1.value())
{

}

if(self.control2.value())
{

}
});

It is working fine whenever either of the control's value changes it is calling. But i need to know due to which control's (among the two) value change, made the computable to gets called. I have checked for any sender like property for ko.computable but no use. Please guide me.

You can use subscription and call a function with shared code:

function update(sender, newVal) {
     // some shared logic
}


self.control1.value.subscribe((newVal) => {
   // you can add here some specific logic for control1
   update(self.control1, newVal); // may be pass some additional params
});

self.control2.value.subscribe((newVal) => {
   // you can add here some specific logic for control2
   update(self.control2, newVal); // may be pass some additional params
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM