Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockoutJS options binding, unable to set value in parent context
    text
    copied!<pre><code>&lt;select data-bind='foreach: theOptions'&gt; &lt;option data-bind='text: name, value: $root.watchThis().something'&gt;&lt;/option&gt; &lt;/select&gt; You have selected &lt;p data-bind="text: watchThis().something"&gt;&lt;/p&gt; Json model. &lt;p data-bind="text: ko.toJSON(watchThis)"&gt;&lt;/p&gt; </code></pre> <p>Options are being shown, but when i change value the relative observable is not updated. More code:</p> <pre><code>function FooModel() { this.something = ko.observable(); } function VM() { var self = this; self.watchThis = ko.observable(new FooModel()); self.theOptions = ko.observableArray(); self.theOptions([ {id:1, name: 'foo'}, {id:2, name: 'bar'}, {id:3, name: 'baz'} ]); } ko.applyBindings(new VM()); </code></pre> <p>I also tried </p> <pre><code>&lt;option data-bind='text: name, value: $root.watchThis().something'&gt;&lt;/option&gt; &lt;option data-bind='text: name, value: $root.watchThis.something'&gt;&lt;/option&gt; &lt;option data-bind='text: name, value: $parent.watchThis().something'&gt;&lt;/option&gt; </code></pre> <p>What seems worst, if i explicitly initialize the value like this</p> <pre><code>function FooModel() { this.something = ko.observable(2); } </code></pre> <p>everything looks fine, but when i switch options the observable is not updated.</p> <p><a href="http://jsfiddle.net/6gvdP/2" rel="nofollow">JSFiddle here</a></p> <p>Question is: how to update an observable from an external context using the option selected value?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload