I've never formally learned javascript, and I feel very much like I'm unaware of 'good' practice.
I'm working on this open source project, and in particular the code that converts the incoming JSON into a usable data structure in this file
The relevent function looks like this:
function start() {
key = "toppage";
utterances = {};
links = {};
colours = {};
icons = {};
labels = {};
slide_number = {};
var req = new XMLHttpRequest();
req.open("GET", "pageset.json");
req.overrideMimeType("application/json");
req.send(null);
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
var obj = JSON.parse(req.responseText);
for (grid in obj.Grid) {
console.log(obj.Grid[grid][0])
labels[obj.Grid[grid][0]] = obj.Grid[grid][1];
utterances[obj.Grid[grid][0]] = obj.Grid[grid][2];
links[obj.Grid[grid][0]] = obj.Grid[grid][3];
icons[obj.Grid[grid][0]] = obj.Grid[grid][4];
colours[obj.Grid[grid][0]] = obj.Grid[grid][5];
slide_number[obj.Grid[grid][0]] = obj.Grid[grid][6];
if(obj.Grid[grid][6]==0){
key=obj.Grid[grid][0]
}
}
grid_size_rows = obj.Settings[0];
grid_size_columns = obj.Settings[0];
setup_messagewindow();
setup_table();
load_page(key);
}
};
//TODO - needs an error message if the JSON doesn't load
}
I have NO idea if this is a reasonable way to populate structures in javascript. It works, but I feel like it could be a hell of a lot more elegant. Any comments people have would be welcome.
setup_*()
are missing. CodeReview does not allow posting partial code with links to GitHub. \$\endgroup\$