Reputation: 242
I'm having trouble with javascript recursion. Here is the code
_parseJson: function($object, $j_array) {
for (i = 0; i < $j_array.length; i++) {
var $el = document_designer.createElement($j_array[i]);
$object.append($el);
if ($j_array[i].elements.length > 0) {
this._parseJson($el, $j_array[i].elements);
}
}
}
Both functions (parseJson
and createElement
) are under the object called "document_designer". Here is the createElement
createElement: function(n) {
var $element = $('<' + n['tag'] + '/>');
document_designer.oi++;
if (n.id == null) {
$element.attr('id', '____element' + document_designer.oi);
}
document_designer.processElement($element, n); // you may ignore this line
return $element;
}
processElement function is not having problems. You may even ignore that line
document_designer._parseJson(document_designer.$preview, json_element)
// $preview is an created object in the `body`
and my parent JSON is : http://www.jsoneditoronline.org/?id=c99c77938e5edf91996e4bb267fd09b1
Root elements are creating and first main's sub childs but other root element's sub elements are not creating
Upvotes: 1
Views: 124
Reputation: 1563
you forgot the var
in
for (i = 0; i < $j_array.length; i++) {
So you are using the same global i
for all calls
change to:
for (var i = 0; i < $j_array.length; i++) {
BTW , I like your code.
Upvotes: 3