Subscribe to Writable Observable

97 views
Skip to first unread message

Watson

unread,
Aug 31, 2011, 11:35:15 AM8/31/11
to KnockoutJS
I'm trying to subscribe to a writable observable and when I do this,
I'm finding that the write operation is being called, but the
subscriptions are never called. Is this a gap in the functionality or
am I just doing something wrong?

Here is an example: http://jsfiddle.net/5egZ8/2/

fla...@gmail.com

unread,
Aug 31, 2011, 1:15:29 PM8/31/11
to KnockoutJS
Hi

The issue is that your underlying data ("testValue") isn't observable,
so writing to it won't trigger any notifications, even if you do wrap
the reads and writes in a dependent observable (there's no observable
for it to depend on, so it's not really adding anything).

Here's a modified version of your code: http://jsfiddle.net/StevenSanderson/VGHKe/
In this version, the underlying data is observable, so everything
updates as you'd expect.

Steve

Watson

unread,
Aug 31, 2011, 1:36:10 PM8/31/11
to KnockoutJS
Well, that much I knew would work. The main problem is that I'm
dealing with a fairly large viewModel such that if I were to use the
mapping plugin to convert all of the necessary values to observables,
it would cause the browser the display the "Stop Running this Script"
message multiple times. The nature of it is data regarding
configuration of all fields on all of the documents in a set of
completely dynamic forms (this is a key requirement in the project). I
could divide this into multiple calls separated using setTimeout, but
I would prefer to only do something like that as a last resort.

What I was trying to do was create a dynamic reference to the current
field being configured (this is a form editor) without converting the
underlying data to observables.

So, if the underlying data must be an observable, then I think I could
just create a single set of observables which would contain the data
for a single field and repopulate these as the field being configured
changes. Granted this is a complete step more convoluted, it's still
way easier that trying to use pure Javascript to do it.

Can you think of a better solution to this? Also, why is it that even
with a writable dependent observable as a wrapper, subscriptions don't
work in my example? I figured that because the data had clearly
changed that the observable was notified by the write operation that
it would make sense to notify subscribers at that time, but obviously
this isn't a usage that you were expecting.
Message has been deleted

studgeek

unread,
Sep 11, 2011, 1:55:15 PM9/11/11
to knock...@googlegroups.com
My current workaround is to create a stub observer for each dependentObserver - see https://groups.google.com/forum/#!topic/knockoutjs/bX8ze5S1w8o.

I do agree we should be able to setup dependencies on models without having to create WiewModels, that doesn't seem to have been the historical focus of KO though. See discussion at https://groups.google.com/forum/#!starred/knockoutjs/XdHjLnDsoPs.
Reply all
Reply to author
Forward
0 new messages