Code
function Product(name) {
this.name = ko.observable(name);
}
function ProductViewModel() {
var self = this;
self.products = ko.observableArray();
$.getJSON("/admin/test", function(allData) {
var mappedProducts = $.map(allData, function(item) { return new Product(item.name) });
self.products(mappedProducts);
console.log(self.products);
});
}
ko.applyBindings(new ProductViewModel());
Problem: while allData
and mappedProducts
are properly set (just an array of products with name and some other field), the line console.log(self.products);
is printing an empty array.
I am really confused, i am at first approach with KO but this seems the very same code from the tutorials... im just using products instead of tasks. I'm sure i'm missing something silly.