I have a knockout template:
<script type="text/javascript" id="myList">
// template stuff here
</script>
I call it in with the foreach option:
<div data-bind="template: { name:'myList', foreach: ItemList }"></div>
I get the view model form my controller and use the following Javascript to bind it:
var viewModel = ko.mapping.fromJS(@Html.Raw(JsonConvert.SerializeObject(Model));
ko.applyBindings(viewModel, document.getElementById("contentsDetails"));
I return a few items from my controller and everything renders fine.
My problem is that if I add to my observable array
var itemToAdd = {
Name: ko.observable("name a"),
Desc: ko.observable("desc a")
}
viewModel.MyObservableArray().push(itemToAdd);
I have checked the array is observable using:
console.log(ko.isObservable(viewModel.MyObservableArray));
It does not update the UI template by adding the new item to the list.
What am I doing wrong here?