This is a sequel. How can I bind ko.observableArray strings?
How to associate an editable observable array of observable rows with a set of input fields? I don't want to bind to an array of objects, since my base JSON sent from the server is an array of strings.
The following example does not work (try http://jsfiddle.net/LDNeA/ ). Associating an array of objects with observed rows is fine, but binding an array of observed rows does not work directly, and the model is not updated.
The important thing is that the entries in the text boxes are mapped back to the model.
JS:
var ViewModel = function() { this.value = ko.observable("hi") this.array1 = ko.observableArray([ko.observable("hi"), ko.observable("there")]); this.array2 = ko.observableArray([{ data: ko.observable("hi") }, { data: ko.observable("there") }]); }; ko.applyBindings(new ViewModel());
HTML:
<div class='liveExample'> <p><input data-bind='value: value' /></p> <div data-bind="foreach: array1"> <p><input data-bind='value: $data' /></p> </div> <div data-bind="foreach: array2"> <p><input data-bind='value: data' /></p> </div> </div> <pre data-bind="text: ko.toJSON($data)"></pre>
source share