javascript - KnockoutJS Populate observableArray using TypeScript -
how can populate observablearray (knockoutjs) using typescript? have class viewmodel.
without typescript load data using $.getjson();
, map it.
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); }); }
this class far, loading data in constructor , json data array. have tried map no luck.
how store/map in list_data = ko.observablearray([]);
in typescript class?
class myviewmodel { constructor() { $.getjson('/home/get', function (data) { alert(data); }); } list_data = ko.observablearray([]); }
thanks
edit:
here data server:
[{ "product": "102289", "artworkid": 19431, "isdownloaded": 1 }, { "product": "272203", "artworkid": 19423, "isdownloaded": 1 }, { "product": "272222", "artworkid": 20306, "isdownloaded": 1 }, { "product": "332245", "artworkid": 19430, "isdownloaded": 1 }, { "product": "382277", "artworkid": 19424, "isdownloaded": 0 }, { "product": "382256", "artworkid": 19425, "isdownloaded": 1 }, { "product": "392272", "artworkid": 19416, "isdownloaded": 1 }, { "product": "422242", "artworkid": 19422, "isdownloaded": 1 }, { "product": "452295", "artworkid": 19414, "isdownloaded": 0 }, { "product": "452219", "artworkid": 19421, "isdownloaded": 0 }, { "product": "452214", "artworkid": 19413, "isdownloaded": 0 }, { "product": "452223", "artworkid": 19415, "isdownloaded": 1 }, { "product": "632204", "artworkid": 20051, "isdownloaded": 1 }, { "product": "632238", "artworkid": 19432, "isdownloaded": 1 }, { "product": "632295", "artworkid": 19419, "isdownloaded": 1 }, { "product": "712220", "artworkid": 19417, "isdownloaded": 1 }, { "product": "722240", "artworkid": 19433, "isdownloaded": 1 }, { "product": "762258", "artworkid": 20273, "isdownloaded": 0 }, { "product": "762278", "artworkid": 20274, "isdownloaded": 1 }, { "product": "792297", "artworkid": 19418, "isdownloaded": 1 }, { "product": "812202", "artworkid": 19429, "isdownloaded": 0 }, { "product": "862280", "artworkid": 19420, "isdownloaded": 1 }]
this how in typescript:
class myviewmodel { list_data = ko.observablearray([]); constructor() { $.getjson('/home/get', data => { var mapped = $.map(data, obj => new aclass(obj)); this.list_data(mapped); }); } }
var myviewmodel = (function () { function myviewmodel() { var _this = this; this.list_data = ko.observablearray([]); $.getjson('/home/get', function (data) { var mapped = $.map(data, function (obj) { return new aclass(obj); }); _this.list_data(mapped); }); } return myviewmodel; })();
Comments
Post a Comment