1

I have a working test example of what I want to do here: http://jsfiddle.net/2BxVk/6/

Instead of the test months I populate my observable array called allMonths now with (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 to do a $.getJSON and insert that into my obs array instead.

This is the output of the $.getJSON source if I view it directly from the 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 to loop through this array I get from the getJSON and put them in allMonths but I cant figure out the correct syntax. I am not interested in tak, styckpris or grundpris, the 3 first properties, I want year, month, ss & ms

I would like something 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 this doesnt work. how can I go through the information json returns and insert them into my observable array as a monthData object type?

3
  • Should work. Could you specify "doesn't work" better? Commented May 21, 2013 at 8:24
  • well there should be 15 rows, 1 for each month added and displayed but they are not displayed. Commented May 21, 2013 at 8:26
  • this is what it says inside the console: Uncaught Error: Unable to parse bindings. Message: ReferenceError: allMonths is not defined; Bindings value: foreach: allMonths Commented May 21, 2013 at 8:29

1 Answer 1

3

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 your fiddle here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.