Iterate through JSON property list

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

    #16
    Ok, took me a few times reading through your code to fully understand it, but I get it now. So basically, you create an exception list for attributes that are problematic or don't add the same in all browsers. If the attribute is in the exception list, it calls _set_node_prop( ), which uses the javascript node[attr] = value method. If the attrib is not in the exception list, it uses the DOM node.setAttribu te(attr, value) method. Makes sense now.

    But why a separate function for exceptions rather than this:
    [code=javascript]
    function set_node_attr(d ocroot, id, attr_state, attr, value)
    {
    var node = docroot.getElem entById(id);
    var exceptions =
    {
    onclick: 1
    };

    if (attr in exceptions)
    {
    node[attr] = value;
    }
    else if (attr_state)
    {
    node.setAttribu te(attr, value);
    }
    else
    {
    node.removeAttr ibute(attr);
    };
    };
    [/code]

    And how would you remove a attr in the exceptions list? Set value to null?

    As for using attachEvent(), I think your example here helped me understand better the addEvent() code I posted (obviously, I didn't write it). I could, for instance, do this:
    [code=javascript]
    addEvent(docume nt.getElementBy Id('image"), "click", someFunction);
    [/code]

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #17
      hi ...

      yes ... that is what i thought :) ... two things:

      first: i used a seperate function because it could be that we might have to use a much more complex handling of some properties ... so that the code would be more modular and we could handle the changes a bit better, or we later could have the need to call the property-setter seperatly from another function ... so i always prefer to use a seperate function for a seperate task ...

      second: to remove a property from the exeption list we have to remove it ... a better way in huge projects would be to use a constant for the list that is defined in a seperate js-file ... so that we may find that very easy and could adapt the list ... setting the value to 0 wouldn't help, since we actually check with the in operator and that simply checks for the existence in the list ...

      kind reagards

      Comment

      • RMWChaos
        New Member
        • Oct 2007
        • 137

        #18
        Why this in your code?

        /**
        * @private
        */

        Comment

        • RMWChaos
          New Member
          • Oct 2007
          • 137

          #19
          gits,

          This is the code I ended up going with in my createDOM() function to address problems adding certain attributes in different browsers:

          [code=javascript]
          for (index in attrib)
          {
          var exceptions = ({
          'onclick' : 1,
          'onmouseover' : 1,
          'onmouseout' : 1
          });
          if (index in exceptions)
          {
          createElem[index] = attrib[index];
          }
          else
          {
          createElem.setA ttribute(index, attrib[index]);
          };
          };
          [/code]

          I understand why you suggested making the exception code a seperate function, but for my purposes, this will work fine.

          I do have one other question, however. You have the exceptions in a JSON list, so I copied what you did, but is that necessary? Why not just:

          [code=javascript]
          var exceptions = new Array ("onclick", "onmouseove r", "onmouseout ");
          [/code]

          There is no data to hold here; so I guess I am missing the point of a JSON property list.

          Thanks!

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #20
            Originally posted by RMWChaos
            Why this in your code?

            /**
            * @private
            */
            that's for marking, that you shouldn't use that method seperatly ... only set_node_attr() ; should be used in the code. the reason for that is, that you later on simply may change the functions without changing the code in different places, and so centralizes the attrib/property setting ion one place ...

            kind regards

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #21
              Originally posted by RMWChaos
              I do have one other question, however. You have the exceptions in a JSON list, so I copied what you did, but is that necessary? Why not just:

              [code=javascript]
              var exceptions = new Array ("onclick", "onmouseove r", "onmouseout ");
              [/code]

              There is no data to hold here; so I guess I am missing the point of a JSON property list.

              Thanks!
              it's not really called a json-list ... its a simple javascript object. have a look at the following simple usage:

              [CODE=javascript]if (index in exceptions) {
              // some code
              }
              [/CODE]
              that wouldn't be possible with an array :) where you would always have to loop through when checking a value. so the current code simply avoids that. and one more note ... simply to mention :) ...

              [CODE=javascript]
              // create an array-instance
              var arr = new Array();

              // equivalent to this :: but js needn't to evaluate
              // the brackets
              var arr = new Array;

              // equivalent to this :: more less to evaluate :) through
              // the use of literals
              var arr = [];
              [/CODE]
              kind regards

              Comment

              • RMWChaos
                New Member
                • Oct 2007
                • 137

                #22
                Hey gits,

                Yeah, I tried it out, and it didn't work. =\

                Sometimes, it's the only way to learn.

                Thanks!

                Comment

                • RMWChaos
                  New Member
                  • Oct 2007
                  • 137

                  #23
                  Originally posted by gits
                  I asked why this:
                  [code=javascript]
                  /**
                  * @private
                  */
                  [/code]
                  You replied:
                  that's for marking, that you shouldn't use that method seperatly ... only set_node_attr() ; should be used in the code. the reason for that is, that you later on simply may change the functions without changing the code in different places, and so centralizes the attrib/property setting in one place.
                  I'm not quite sure what you mean. Maybe it would be easier if I learned Deutsch. =D I think you mean by using that code snippet, it forces all functions that come after it to only be used in conjunction with the function before it, or is it just a reminder to the programmer?

                  As for util-method...Ok, ok, ok, you win! I will make these changes:

                  [code=javascript]
                  //in changeAttrib
                  function changeAttrib(id , attr, value))
                  {
                  set_node_attrib (id, attr, value, false);
                  };

                  // in createDOM()
                  for (index in attrib)
                  {
                  set_node_attrib (attrib.id, index, attrib[index], true);
                  };

                  // in set_node_attrib
                  function set_node_attr(i d, attr, value, state)
                  {
                  var node = docroot.getElem entById(id);
                  var exceptions =
                  {
                  'onclick' : 1,
                  'onmouseover' : 1,
                  'onmouseout' : 1
                  };
                  if (attr in exceptions)
                  {
                  set_node_except ions();
                  }
                  else if (state)
                  {
                  node.setAttribu te(attr, value);
                  }
                  else
                  {
                  node.removeAttr ibute(attr);
                  };
                  };

                  /**
                  * @private
                  */

                  function set_node_except ions(node, attr, value)
                  {
                  node[attr] = value;
                  };
                  [/code]

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #24
                    it's a reminder as you said ... and the programmer should pay attention to it :) it's always a good idea to handle things that mix up different things like our attribute/property example in one place ... respectively with one single method-call ... so that everytime you have to build in more exeptions you have only one point where you have to maintain the code ;) ...

                    kind regards

                    Comment

                    • RMWChaos
                      New Member
                      • Oct 2007
                      • 137

                      #25
                      Originally posted by gits
                      it's always a good idea to handle things that mix up different things like our attribute/property example in one place ... respectively with one single method-call ... so that every time you have to build in more exceptions you have only one point where you have to maintain the code
                      I understand exactly what you are saying, but I have a problem in using this code. The util-method assumes that the node-id already exists, but in the case of my createDOM script, it does not. The attribs are being set immediately before the node is actually created, so that the node is created with the attribs. Hm, let me see if I can create the node, then set the attribs. That might work...testing. ..

                      Nope, didn't work. Again, the problem is that no attribs have been set, including the node id, so that document.getEle mentById(id) has nothing to find. If I am going to use your util-method, I really need to give this some thought.

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5388

                        #26
                        simply change it to pass the node itself to it instead of the id :)

                        kind regards

                        Comment

                        Working...