i've a problem with parsing json data from a callback function as a function parameter.
calling the function buildUL(ul, getJsonData()); is the problem because getJsonData(); dont return the json data. it has it's own return.
how to code the callback function the right way?
function getJsonData()
{
$.ajax({
async: false,
url: 'ajax/menu.json',
success: buildData
});
// return false;
}
;
function buildData(jsonData) {
var data = jsonData;
//alert(data);
var source = [];
var items = [];
// build hierarchical source.
for (i = 0; i < data.length; i++) {
var item = data[i];
var label = item["text"];
var parentid = item["parentid"];
var id = item["id"];
if (items[parentid]) {
var item = {parentid: parentid, label: label, item: item};
if (!items[parentid].items) {
items[parentid].items = [];
}
items[parentid].items[items[parentid].items.length] = item;
items[id] = item;
}
else {
items[id] = {parentid: parentid, label: label, item: item};
source[id] = items[id];
}
}
return source;
}
;
function buildUL(parent, items) {
$.each(items, function () {
if (this.label) {
// create LI element and append it to the parent element.
var li = $("<li>" + this.label + "</li>");
li.appendTo(parent);
// if there are sub items, call the buildUL function.
if (this.items && this.items.length > 0) {
var ul = $("<ul></ul>");
ul.appendTo(li);
buildUL(ul, this.items);
}
}
});
}
;
var ul = $("<ul></ul>");
ul.appendTo("#testmenu");
buildUL(ul, getJsonData());
error: TypeError: a is undefined