Having trouble looping through an object using foreach in my code - the struggle is real after 6 months of not coding!
This code snippet works perfectly:
<div data-bind="text: $root[36].partition"></div>
But I can't seem to get the foreach loop to work:
<div data-bind="foreach: $root">
<div data-bind="text: $data.partition"></div>
</div>
When I check my HTML, all I see is:
<div data-bind="foreach: $root"></div>
The JSON data from the PHP script is structured like this, with partition IDs 09, 10, and 36:
top level
09
partition
vip
10
partition
vip
36
partition
vip
Just playing around with my JS code, nothing fancy:
$(document).ready(function() {
var viewModel = {};
$.getJSON('/lbstat/read.php', function(data) {
viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
});
Here's a snippet of the JSON data:
{"23":{
"partition":"Prod New SVCs Partition",
"env_dc":"Prod",
"hosts":["server01.domain.com", "server02.domain.com"],
"vips":{
"124":{
"dc_endpoint":"ADX - Prod - Intranet",
"gw_port":"9007",
"vip_name":"adx-prd.domain.net"
},
"210":{
"dc_endpoint":"Msg - Prod - Internet",
"gw_port":"8013",
"vip_name":"messaging-prd.domain.com"
},
"211":{
"dc_endpoint":"Msg - Prod - Intranet",
"gw_port":"9013",
"vip_name":"messaging-prd.domain.net"}
},
}
}
Full JSON available here: http://pastebin.com/zpNngr53
Can you help me figure out what I'm doing wrong here?