Is there a way to populate an observableArray in KnockoutJS using TypeScript? My ViewModel is defined as a class.
In the absence of TypeScript, I would typically load the data using $.getJSON();
and then map it accordingly.
function ViewModel() {
var self = this;
self.list_data = ko.observableArray([]);
$.getJSON('/Home/Get', function (data) {
var mapped = $.map(data, function (obj) { return new AClass(obj); });
self.list_data(mapped);
});
}
I have attempted to incorporate this data loading process within the constructor of my TypeScript class. However, I am encountering difficulties when attempting to map the JSON data array and store it within the
list_data = ko.observableArray([]);
property. Can anyone provide guidance on how to accomplish this?
class MyViewModel {
constructor() {
$.getJSON('/Home/Get', function (data) {
alert(data);
});
}
list_data = ko.observableArray([]);
}
Thank you for your assistance.
EDIT:
The following is the data received from the server:
[{ "Product": "102289", "ArtworkId": 19431, "IsDownloaded": 1 },
{ "Product": "272203", "ArtworkId": 19423, "IsDownloaded": 1 },
{ "Product": "272222", "ArtworkId": 20306, "IsDownloaded": 1 },
...
{ "Product": "862280", "ArtworkId": 19420, "IsDownloaded": 1 }]