Knockout subscribe: How to avoid two calls in subscribe when the subscribed observable is cleaned?

StackOverflow https://stackoverflow.com/questions/11319630

  •  18-06-2021
  •  | 
  •  

Question

I'm triying to figure out how to avoid in a cleaner way recursivity with a subscribe.

In the next fiddle show how 2 times the subscribe is called (for observableOne), this happends because I need the observable (that is monitoring subscribe) in the initial state.

It could be avoid with an "if" conditional(observableTwo), but I'm wondering if are some function that do this task instead "if" conditional. I tried with cleanNode but doesn't work.

This is the fiddle: http://jsfiddle.net/wabe/nLYyf/

I don't want to unsubscribe the observable because it should be listening after the call.

Was it helpful?

Solution

one part of the problem, if you set the setting, it doesn't seem to trigger the databinding so the checkbox can end up in the wrong state.

to fix this... rather than set observableTwo, put it in a timeout...

setTimeout(function(){ self.observableTwo(''); },0);

for how to suppress the recursive notification, I don't see any "official" way. So the way you are doing it, or other variations where you might use a flag, or maybe come up with a generic wrapper function that sorts it out for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top