Self-aware bindings

85 views
Skip to first unread message

Armin Burkholder

unread,
Jan 24, 2013, 5:37:33 PM1/24/13
to knock...@googlegroups.com
Maybe someone has a solution to this, but I can't find a public API call that will solve this problem.

Here is a fiddle with explanation: http://jsfiddle.net/aburkholder/RnJ2R/1/

Basically, I need to be able to notify sibling bindings on an element to update. Is there anything in the KO api that would do this?

david...@bytesurgery.com

unread,
Jan 24, 2013, 6:31:55 PM1/24/13
to knock...@googlegroups.com
Hi Armin,

Seems like a sort-of unusual way to do this in Knockout.
Because you've written test.hasError() the function will only get called once (and it's undefined initially).
Knockout doesn't know to keep checking it because hasError is not observable or computed.
In my updated jsFiddle I add hasError as a ko.computed once in the init() function. You could also create it as an observable.


Thanks,
David

Armin Burkholder

unread,
Jan 24, 2013, 7:06:17 PM1/24/13
to knock...@googlegroups.com, david...@bytesurgery.com
Thanks for the response, David.  And frankly, I am a little mystified now.

You will notice that hasError is declared as an observable at the top of the script. My fiddler probably doesn't make it very clear, but hasError is intended to be a generic indicator for any observable. My expectation was that changes made within any bindingHandler would cause notification to sibling bindings when it changed.

So, your solution solves the immediate problem for this particular bindingHandler. But the state of hasError is now dependent on a single definition of what constitutes an error condition and is no longer generic or reusable. I had hoped there would be something in the API that would allow me to force sibling bindings to update - I would be okay with having to do that manually, but I haven't yet found a mechanism to make it happen.

kevin....@gmail.com

unread,
Jan 24, 2013, 8:03:58 PM1/24/13
to knock...@googlegroups.com, david...@bytesurgery.com
I think I had a similar issue previously, see the response I got here: https://groups.google.com/d/msg/knockoutjs/fQt9W8W11PY/3g0sW8_3gPwJ

as for your use case, can you make use of the knockout validation library?  

Kevin

Michael Best

unread,
Jan 24, 2013, 8:05:58 PM1/24/13
to knock...@googlegroups.com
We are working on solving the underlying cause of this problem in Knockout (https://github.com/SteveSanderson/knockout/issues/321) and hope to have this done in a couple months in a 3.0 release. For now, you can use my Freedom plugin (https://github.com/mbest/knockout-freedom/) that will solve the problem but may have some compatibility issues. Here's your example with the Freedom plugin included: http://jsfiddle.net/mbest/RnJ2R/5/

-- Michael

Armin Burkholder

unread,
Jan 25, 2013, 11:52:53 AM1/25/13
to knock...@googlegroups.com
Thank you Michael!  I am glad to see you are on top of this.  The plugin will suffice for now, and I look forward to the release 3.0.
Best regards,
Armin
Reply all
Reply to author
Forward
0 new messages