javascript - populate observable array from json -
i have working test example of want here: http://jsfiddle.net/2bxvk/6/
instead of test months populate observable array called allmonths (like this)
self.tak = ko.observable(100); self.styckpris = ko.observable(10); self.grundpris = ko.observable(500); self.allmonths = ko.observablearray([ new monthdata(2013, 1, 412, 142, self), new monthdata(2013, 2, 112, 642, self), new monthdata(2013, 2, 100, 742, self), new monthdata(2013, 3, 6513, 69, self), new monthdata(2013, 4, 34, 211, self), new monthdata(2013, 5, 123, 435, self), new monthdata(2013, 6, 412, 142, self), new monthdata(2013, 7, 412, 142, self) ]);
i want $.getjson , insert obs array instead.
this output of $.getjson source if view directly url:
[{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":2,"ss":784,"ms":576,"count":1360,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":3,"ss":977,"ms":636,"count":1613,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":4,"ss":1040,"ms":726,"count":1766,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":5,"ss":1373,"ms":1013,"count":2386,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":6,"ss":856,"ms":612,"count":1468,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":7,"ss":594,"ms":299,"count":893,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":8,"ss":1261,"ms":826,"count":2087,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":9,"ss":1092,"ms":729,"count":1821,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":10,"ss":1097,"ms":747,"count":1844,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":11,"ss":872,"ms":706,"count":1578,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2012,"month":12,"ss":329,"ms":110,"count":439,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2013,"month":2,"ss":911,"ms":0,"count":911,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2013,"month":3,"ss":1002,"ms":0,"count":1002,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2013,"month":4,"ss":1157,"ms":0,"count":1157,"price":20000},{"tak":2700,"styckpris":35,"grundpris":20000,"year":2013,"month":5,"ss":852,"ms":421,"count":1273,"price":20000}]
i have been trying loop through array getjson , put them in allmonths cant figure out correct syntax. not interested in tak, styckpris or grundpris, 3 first properties, want year, month, ss & ms
i like:
var allmonths = ko.observablearray(); $.getjson('simulera/monthdata', function (data) { $.each(data, function (i, val) { allmonths.push( new monthdata(val.year, val.month, val.ss, val.ms, self) ); }); });
but doesnt work. how can go through information json returns , insert them observable array monthdata object type?
try adding self
.
$.each(data, function (i, val) { self.allmonths.push( new monthdata(val.year, val.month, val.ss, val.ms, self) ); });
a working version of fiddle here.
Comments
Post a Comment