Partially iterating through JSON list looses attrib properties

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RMWChaos
    New Member
    • Oct 2007
    • 137

    Partially iterating through JSON list looses attrib properties

    The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?"

    Alright, here's the story: with a great deal of help from gits, I've developed a DOM creation and deletion script, which can be used in multiple applications. You simply feed the script a JSON list of any size, and the script will create multiple DOM elements with as many attributes as you desire, iterating through all the elements in the list one after another. An additional feature allows you to select individual elements from the list rather than iterating through all of them at once. Okay, everything is good up to this point.

    Now I've gone and asked my poor, overworked script to take on even more responsibility, and apparently it is just too much for the script to bear. I wanted to combine the individual element creation with the iteration functionality. Oh boy, let the fun begin!

    If my JSON property lists were static, this whole issue would be moot. Unfortunately, the lists are dynamic (I like to make things difficult). For example, each element has a unique id that is determined by its function, location on the displayed HTML page, and other factors. Here is an example:

    [code=javascript]
    var page =
    {
    'homepage' : 0,
    'pagetwo' : 1,
    'pagethree' : 2
    };

    for (var index in page)
    {
    var imageId = "nav" + index;
    var navOptions =
    {
    'id' : imageId,
    'dom' : 'img',
    'parent' : 'body'
    };
    createDOM(navOp tions);
    };
    [/code]

    Here you can see that imageId is dynamically created each time the for loop is run. That is fine as long as I am iterating through the entire list, but if I want to stop at a certain point (say at homepage) and later iterate through the rest (pageone and pagetwo), then everything breaks down because of the following code:

    [code=javascript]
    // Split attribList and submit to createNode //
    /*
    * 'attribList' (Str.) is the full JSON list passed to createDOM
    * 'split' (#) is the first element of the iteration section of the list
    * 'iterate' (#) is the current element to be created
    */
    function createDOM(attri bList, split, iterate)
    {
    var splitList = {};
    // if only split is listed, default iterate to 0 //
    if (split && (typeof iterate == 'undefined'))
    {
    iterate = 0;
    };
    // if neither split nor iterate, create entire attribList (no split) //
    if ((typeof split == 'undefined') || (split == 0))
    {
    createNode(attr ibList);
    }
    // if iterate is less that split or if split is null, create only value [iterate] per index //
    else if ((split > iterate) || (split == null))
    {
    for (var index in attribList)
    {
    var value = attribList[index];
    splitList[index] = value instanceof Array ? value[iterate] : value;
    };
    createNode(spli tList);
    }
    // if iterate matches or exceeds split, create the remainder of values //
    else if (split <= iterate)
    {
    for (var i = iterate; i < attribList.id.l ength; i++)
    {
    for (var index in attribList)
    {
    var value = attribList[index];
    splitList[index] = value instanceof Array ? value[i] : value;
    };
    createNode(spli tList);
    };
    };
    };
    [/code]

    Originally, this was part of my createDOM() code, the very beginning in fact, but I am starting to think it needs to be attached to the script that is calling createDOM(). Was trying to remove redundancy since multiple scripts call this code, but because there is no easy way to pass all the vars and other JSON lists or Arrays that create the dynamic objects in the attribList...we ll, I may not have any alternative. I need to think about this some more. Maybe iteration recalls the original function to run again with a new variable ... originalFunctio n(i++)...hmm.
  • RMWChaos
    New Member
    • Oct 2007
    • 137

    #2
    Oh wait, duh! <smacks forehead>

    All I need to do is call the code currently called createDOM() inside my other scripts in order to create the new attribList, then pass attribList to the code that actually creates the DOM elements as many times as there are nodes to iterate through.

    Sigh. Sometimes I just need to talk it out with you folks in order to understand my folly. =D

    I'll report back here on the results.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      hey ... :)

      i'm looking forward to it ;)

      kind regards

      Comment

      • RMWChaos
        New Member
        • Oct 2007
        • 137

        #4
        Originally posted by gits
        hey ... :)

        i'm looking forward to it ;)

        kind regards
        Don't hold your breath. =D

        Still having problems, but I think I know what I need to do, I just need to figure out how to do it.

        Instead of submitting the entire JSON list to createDOM() and splitting the list there, I need to split the list before I send it to createDOM(). The problem is that I need to include the code within the same function where all my vars are that create the JSON list in the first place, and then I still want to somehow make it modular rather than have to write the same code repeatedly within the scripts.

        WAIT! Epiphany! A single, large JSON list, with deeper member levels of course, then just singly pick out the node to create. Iteration code is separate, but operates on the exact same principal of: for (i = #; i < #; i++)...hm, that could work, but talk about a major recode! Still, could be very efficient.

        You know, I don't think I'll ever finish this darn website. I still have to code an XML (more probably JSON) -based forums system after the framework for the site is done. Never could get chatterbox to work.

        Comment

        • RMWChaos
          New Member
          • Oct 2007
          • 137

          #5
          Ok, here's a start...much simplified:

          [code=javascript]
          // Split attribList and submit to createDOM //
          function splitAttribList (attribList, splitStart, splitStop)
          {
          for (var i = splitStart; i <= splitStop; i++)
          {
          var splitList = {};
          for (var index in attribList)
          {
          var value = attribList[index];
          splitList[index] = value instanceof Array ? value[i] : value;
          };
          createDOM(split List);
          };
          };
          [/code]

          This will work for a single level JSON list. But what if it's multiple levels:

          [code=javascript]
          myJsonList = {
          'navigation': [
          {
          'id' : 'idOne',
          'dom' : 'img',
          'parent': 'body',
          'src' : '../images/imageOne.jpg'
          },{
          'id' : 'idTwo',
          'dom' : 'img',
          'parent': 'body',
          'src' : '../images/imageTwo.jpg'
          },],
          'loginForm' : [
          {
          'id' : 'login',
          'dom' : 'img',
          'parent': 'body',
          'src' : '../images/login.jpg'
          },{
          'id' : 'logout',
          'dom' : 'img',
          'parent': 'body',
          'src' : '../images/logout.jpg'
          },],
          };

          myJsonList.logi nForm[1].id // 'logout'
          [/code]

          So now the hard part: convert the code above to access this JSON structure...

          [code=javascript]
          /*
          * Split attribList and submit to createDOM
          * attribList is original JSON list
          * member is 1st level member (by name or #) of attribList
          * splitStart is begin object by node # (0, 1, 2...) only!
          * splitStop is end object by node # (0, 1, 2...) only!
          * if splitStart and splitStop are == only one node is created.
          */
          function splitAttribList (attribList, member, splitStart, splitStop)
          {
          // iterate from start to stop //
          for (var i = splitStart; i <= splitStop; i++)
          {
          // new empty object //
          var splitList = {};
          // create all attribs in object member[i] //
          for (var index in attribList[member][i])
          {
          var value = attribList[member][i];
          // create new JSON list with single node and attribs //
          splitList[index] = value instanceof Array ? value[index] : value;
          };
          // submit new list to createDOM() //
          createDOM(split List);
          };
          };

          // to initiate
          splitAttribList (myJsonList, loginForm (or 1), 0, 0); // this should create 'login' node //
          [/code]

          Is that right? If it is, that wasn't so hard after all. Just wrote it here, so I still have to test. I wonder if the structure of splitList will match the structure of the original JSON list, or if it will be a single-level list?? I think single...

          Now if this works, how to convert the vars into strings in the original JSON list so that each attrib has a unique value? Sigh...more work.

          Comment

          Working...