|dependentObservable doesn't call notifySubscribers on write||studgeek||9/11/11 10:07 AM|
I've come across what I think is surprising behavior for dependentObservables, they actually don't call notifySubscribers on write. They only call them the first time they are read.
I have created a simple example at http://jsfiddle.net/studgeek/85aEN/ based on the discussion in https://groups.google.com/d/topic/knockoutjs/NhLtoFJpwpU/discussion talks about this issue as well.
I'm wondering why this is, it seems there is an assumption that you will always have an underlying observable which seems a to be an unnecessary and a limiting design. There are cases where you don't own the underlying state, in those cases to properly bind to that state you either have to copy the state to an observable or create a fake observable that you trigger on each write.
Also, if dependentObservables don't notifySubscribers on write, why do they even bother to do it on read? They get added to the observables list and subscribed to, but then they never trigger on updates. So what is the point of subscribing to them at all?
Is there some downside to calling notifySubscribers in the write that I am missing?
|unk...@googlegroups.com||9/11/11 10:49 AM||<This message has been deleted.>|
|Re: dependentObservable doesn't call notifySubscribers on write||studgeek||9/11/11 10:53 AM|
I'll add the workaround I am currently using is a stub observable as I show here
http://jsfiddle.net/studgeek/9zuFJ/, but it does feel to me like DOs should just do the notification themselves.
|Re: dependentObservable doesn't call notifySubscribers on write||studgeek||9/11/11 12:24 PM|
In the 1.3 beta, this for write/read is bound to undefined by default so my original example of keeping triggerObservable on the literal no longer works (see https://groups.google.com/forum/#!topic/knockoutjs/75YCYgp32-s for discussion).
I have updated the example to create the triggerObserver in a dynamic scope instead. It's the new base at http://jsfiddle.net/studgeek/9zuFJ/, the original example that is simpler to follow (but only works with 1.2.1) is at http://jsfiddle.net/studgeek/9zuFJ/3/.
|Re: dependentObservable doesn't call notifySubscribers on write||rpn||9/11/11 12:57 PM|
I think that for your scenario, dependentObservables may not be the right tool for the job. dependentObservables are set up to detect dependencies in the read function and re-evaluate/notify whenever any of those dependencies change. In a writeable dependentObservable, the write function is really just a place to intercept the write and allow you to set any observables necessary, such that your read function would return the proper value (write is typically the reverse of read in most cases, unless you are transforming the value).
For your case, I would personally use an observable to represent the value and then a manual subscription to that observable to update the original value (the one that you may not have control over).
It would be like: http://jsfiddle.net/rniemeyer/Nn5TH/