Loading ...
Sorry, an error occurred while loading the content.

40287Re: [ydn-javascript] Is it possible to setup TreeView to keep it's state?

Expand Messages
  • Satyam
    Nov 3, 2008
      You are missing the empty parenthesis at the end of getTreeDefinition,
      it should be:

      var tree_struct_x = tree.getTreeDefinition();


      Without the parenthesis you are not assigning the tree definition to
      tree_struct_x, which is what you want, but you are assigning it a
      reference to the function, thus the alert is showing the string
      representation of that function itself, which is its code. A JSON
      string should not contain executable code and that is why stringify
      refuses to convert it.

      In JavaScript, functions are objects of class Function. As objects,
      they can be referenced by variables. What you usually refer to as the
      'function name' is a concept that really doesn't exist in JavaScript, a
      function is referred to by the name of the variable that is pointing to
      it. As any other object, you can copy a reference to that instance of
      the Function object (ie: the (lowercase) function) from one variable to
      the next. The method toString of class Function renders the body of the
      function. As you can see in your alert() below, there is no name as
      part of the function since functions have no intrinsic names. The ()
      operator, applied to an instance of a Function object, executes it.
      Trye this:

      var tree_struct_x = tree.getTreeDefinition;
      alert(tree_struct_x());

      See the parenthesis within the alert? The first statement copies a
      reference to the function, not the result of executing it so
      tree_struct_x now becomes a 'function' (actually, a new reference to the
      same Function instance) which you can execute at any time by appending
      the parenthesis to it. Anyway, this is not what you want to do, it was
      just to prove the point. You simply put the parenthesis in the first
      statement as shown above.

      Satyam

      Lee.M wrote:
      > On Nov 2, 2008, at 1:53 AM, Satyam wrote:
      >
      >
      >> Cookies have to be strings or things that have string representations.
      >> The string representation of an object is "[object]"which is not very
      >> useful.
      >>
      >> You have to use the JSON utility to convert the tree definition to a
      >> string and store that string into the cookie. On the way back, you
      >> read
      >> the cookie, parse it with the JSON utility and use that as the second
      >> argument to the TreeView constructor.
      >>
      >>
      >
      > Thanks again :) I had also tried that but this is the result:
      >
      > All of this is in <head> along with the treeview and cookie .js's:
      >
      > <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo/yahoo-min.js
      > "></script>
      > <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/json/json-min.js
      > "></script>
      > ...
      > var tree_struct_x = tree.getTreeDefinition;
      > alert(tree_struct_x);
      > var tree_data_string_x =
      > YAHOO.lang.JSON.stringify(tree_struct_x);
      > alert(tree_data_string_x);
      > ...
      >
      > 1st alert is:
      > function ()
      > {
      > return this.getRoot().getNodeDefinition();
      > }
      >
      > 2nd one is:
      > undefined
      >
      > Seems om as per: http://developer.yahoo.com/yui/json/#stringify
      >
      > What am I doing wrong ?
      >
      > TIA!
      >
      > Lee.M
      >
      > ------------------------------------
      >
      > Yahoo! Groups Links
      >
      >
      >
      > ------------------------------------------------------------------------
      >
      >
      > No virus found in this incoming message.
      > Checked by AVG - http://www.avg.com
      > Version: 8.0.175 / Virus Database: 270.8.5/1762 - Release Date: 02/11/2008 9:51
      >
      >
    • Show all 21 messages in this topic