I have an AJAX request that returns an array of objects (Results). In the success callback function, the binding is applied using:
success: function(data) {
ko.applyBindings(new ResultsViewModel(data), target);
}
Where target is the target DOM element, and ResultsViewModel is created similar to:
function ResultsViewModel(model) {
var self = this;
self.text = ko.observable(model.text);
self.id = model.id;
self.Descriptions = ko.observableArray(model.descriptions);
}
In my HTML, the data is bound to the elements using:
<div data-bind="template: {name: 'results-template', foreach: $data}">
Then a template class that binds the individual properties of the view model.
My question is: how can I bind the array of data that is returned by my AJAX call (multiple result objects) using the viewmodel with a mixture of observable and static properties to the DOM?
The foreach loop I am using should create a template and bind each object in the returned array to a separate div, but it looks as though it is attempting to bind the root object in the returned array, which is null (unnamed array).
modelis like, and a few more bits of your view (specifically: the template).