I am trying to write a bit of UI that allows the user to either enter a number via a textbox or pick an option from a drop down.
I have made a simplified jsFiddle at http://jsfiddle.net/unklefolk/PNQeR/2/
As you can see:
- When "The number" is selected you can enter a text into a textbox.
- When "The option" is selected you can select one of two options from a drop down
The textbox and the drop down are BOTH bound to the ItemValue
property of item in the viewModel
. Although the code seems to be working I am getting errors. If you fire up the debug window in Chrome, when you change the first drop down you get the error:
Uncaught TypeError: Object 0 has no method 'ItemName'
I believe this is happening in the ItemText
dependentObservable (aka computed).
this.ItemText = ko.dependentObservable(function () {
return _isItemAConstant() === 'true' ? this.ItemValue() : this.ItemValue().ItemName();
}, this);
Clearly the ItemName()
function is being called on the numeric value '0' causing the error.
What can I do so that this error does not occur? Is my design of binding two controls to the same observable a fundamental error?