[Dojo-interest] global variable value not retained within load function

Jim openbip at gmail.com
Mon Feb 5 09:31:27 MST 2007


Aha, I think you're losing a race-condition, here.

Chronologically, I'm pretty sure this is what's happening:
1.  trunkGroup is initialized to an empty array of size 0
2.  getTrunkGroups() is called
3.  The AJAX call is *initiated*
4.  Your document.write(...) call references the first element in your 
array, which doesn't exist yet
5.  The AJAX call returns, and trunkGroup is filled.

You want to swap steps 4 and 5, of course.

Instead of using document.write(...), I would so something like the 
following:

<body>
<span id="firstElementOfArray">Please wait...</span>
</body>

... and have your "load" function change the display accordingly:
  load: function(type, data, evt){
     trunkGroup = eval(data);
     dojo.byId("firstElementOfArray").innerHTML = "" + trunkGroup[0];
     alert(trunkGroup[0]); // this outputs just fine
    },

The user will see "Please wait..." until your AJAX call returns, at 
which point it would then change to what you're looking for.

-- Jim


Meng-Shyang Teng wrote:
> Jim,
>
> Thanks for the prompt reply.  Here's my code in its entirety:
>
> Notice I did not re-declare trunkGroup inside of the function.  If 
> in-line JavaScript is executed as the page is parsed
> top-to-bottom, then shouldn't calling the function getTrunkGroups() 
> before the body tag generate the correct value for trunkGroup?
>
> Please advise.
>
>
> <html>
> <head>
>  <title>Example</title>
>  <script language="javascript" src="js/dojo/dojo.js"></script>
>  <script language="javascript">
>   dojo.require("dojo.io.*");
>   dojo.require("dojo.event.*");
>
>   var trunkGroup = new Array();
>
>   function getTrunkGroups() {
>    var params = new Array();
>
>    params['cfgType']="AscendentTrunkCfg";
>    params['cmd']="get";
>    params['xquery']="//TRUNK_RESERVE_METHOD/TrunkGroup";
>    params['cache']="no";
>
>    var bindArgs = {
>     url:  "/xtools/servlet/processXMLCfg",
>     error: function(type, data, evt){
>      alert(data);
>     },
>     load: function(type, data, evt){
>      trunkGroup = eval(data);
>      alert(trunkGroup[0]); // this outputs just fine
>     },
>     mimetype: "text/json",
>     content: params
>    };
>
>    var requestObj = dojo.io.bind(bindArgs);
>   }
>   getTrunkGroups();
>  </script>
> </head>
> <body>
> <p>
> <script language="javascript">
> document.write(trunkGroup[0]);  // this outputs "undefined"
> </script>
> </body>
> </html>
>
>
>
> On 2/5/07, *Jim* < openbip at gmail.com <mailto:openbip at gmail.com>> wrote:
>
>     Could you paste your entire page?  It's difficult to tell if you
>     have a
>     scoping issue or a timing issue.
>
>     Remember that in-line JavaScript is executed as the page is parsed
>     top-to-bottom, and /then/ any onload functionality will be
>     executed ...
>     so if you're initializing trunkGroup inside dojo.addOnLoad(...), that
>     initialization would not happen until after your document.write(...)
>     call below.
>
>     <body>
>     <script language="javascript">
>     document.write(trunkGroup[0]);  // outputs "undefined"
>     </script>
>     </body>
>
>
>     I can tell you that the following executes "array" "string" "string",
>     showing that you can indeed manipulate global variables in
>     more-specific
>     scopes.
>
>     var bip = "array";
>
>     function bip2 () {
>       this.bip3 = function() {
>         bip = "string";
>         alert(bip);
>       }
>     };
>
>     dojo.addOnLoad(
>         function() {
>           alert(bip);
>           new bip2().bip3();
>           alert(bip);
>         }
>       );
>
>
>     One final note, using "var" declares that variable specifically to its
>     containing scope.  So if you're doing:
>     dojo.addOnLoad (
>         function() {
>           var trunkGroup ...
>         });
>
>     ... then "trunkGroup" is not going to be global, but a private
>     member-variable of the anonymous class you're passing into the
>     addOnload
>     function.
>
>
>     -- Jim
>
>
>     Meng-Shyang Teng wrote:
>     > Within the load function, I have the following:
>     >
>     > var trunkGroup = new Array();  // global variable defined here
>     > function getTrunkGroups() {
>     > .. // some code omitted here
>     >
>     > var bindArgs = {
>     > load: function(type, data, evt){
>     >  trunkGroup = eval(data);  // output correct value
>     >  alert (trunkGroup[0]);
>     >   }
>     > }
>     > ..
>     > }
>     >
>     >
>     > I am attempting to assign a global variable
>     trunkGroup.  Although the
>     > alert(trunkGroup[0]) does return correct values, the trunkGroup
>     is no
>     > longer defined within the html document.  Is there a way to
>     assign a
>     > global variable inside of the load function?
>     >
>     > I have the following code inside of my html:
>     >
>     > <body>
>     > <script language="javascript">
>     > document.write (trunkGroup[0]);  // outputs "undefined"
>     > </script>
>     > </body>
>     >
>     ------------------------------------------------------------------------
>     >
>     > _______________________________________________
>     > Dojo FAQ: http://dojo.jot.com/FAQ
>     > Dojo Book: http://manual.dojotoolkit.org/DojoDotBook
>     > Dojo-interest at dojotoolkit.org <mailto:Dojo-interest at dojotoolkit.org>
>     > http://dojotoolkit.org/mailman/listinfo/dojo-interest
>     >
>
>     _______________________________________________
>     Dojo FAQ: http://dojo.jot.com/FAQ
>     Dojo Book: http://manual.dojotoolkit.org/DojoDotBook
>     Dojo-interest at dojotoolkit.org <mailto:Dojo-interest at dojotoolkit.org>
>     http://dojotoolkit.org/mailman/listinfo/dojo-interest
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Dojo FAQ: http://dojo.jot.com/FAQ
> Dojo Book: http://manual.dojotoolkit.org/DojoDotBook
> Dojo-interest at dojotoolkit.org
> http://dojotoolkit.org/mailman/listinfo/dojo-interest
>   



More information about the Dojo-interest mailing list