Dear Keve,

Your problem is with the scope of variables and the closures you generate
(the listener function you define generates a closure because it references
variables from outside the function).
http://stackoverflow.com/questions/643542/doesnt-javascript-support-closures-with-local-variables/643664gives
some more explanation, and some hints to a possible solution

-- 
Rob

On Sun, Feb 23, 2014 at 8:58 PM, Keve Nagy <k...@safe-mail.net> wrote:

> Good Evening Everyone!
> I need a little help from someone more experienced in JavaScript than I am.
> A row of my qx objects (a SelectBox, a Widget, and another SelectBox) are
> needed to be repeated on screen multiple times, and I am trying to define
> them using a loop.
> My plan is to define an array, and each element of the array will
> represent a row of objects.
> The problem I face is with the generation of the changeSelection
> event-handler code.
>
> Example:
>
>         var ri = 0;  // arrai index
>         var myObjectArray = [];
>         for (ri=0; ri < 20; ri++) {
>                 myObjectArray[ri] = {};
>                 myObjectArray[ri].Wgt = new
> qx.ui.core.Widget().set({width:15, height:15, backgroundColor:"white"});
>                 myObjectArray[ri].colorSelect = new qx.ui.form.SelectBox();
>                 myObjectArray[ri].nameSelect = new qx.ui.form.SelectBox();
>                 //
>
> myObjectArray[ri].colorSelect.addListener("changeSelection", function(e) {
>                         myObjectArray[ri].Wgt.setBackgroundColor("blue");
>  //  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>                 }); // end addListener
>                 //
>                 myWindow.add(myObjectArray[ri].nameSelect,  {column:0,
> row:ri});
>                 myWindow.add(myObjectArray[ri].Wgt,         {column:1,
> row:ri});
>                 myWindow.add(myObjectArray[ri].colorSelect, {column:2,
> row:ri})
>         } // end for loop
>
>
> The trouble is with the line marked by <<< arrowheads.
> This works fine when the event-handler is defined, but fails to run
> properly when triggered because
> myObjectArray[ri].Wgt.setBackgroundColor("blue") uses a variable "ri" for
> the array index, which has a different value at runtime from the value at
> definition time.
> If I do it manually, 20 times, once for each myObjectArray element and use
> hardcoded array index values instead of a variable, then it all works fine.
>
>
> myObjectArray[0].colorSelect.addListener("changeSelection", function(e) {
>                         myObjectArray[0].Wgt.setBackgroundColor("blue");
>  //  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>                 }); // end addListener
>
>
> myObjectArray[1].colorSelect.addListener("changeSelection", function(e) {
>                         myObjectArray[1].Wgt.setBackgroundColor("blue");
>  //  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>                 }); // end addListener
>
>                 ...
>
>
> myObjectArray[19].colorSelect.addListener("changeSelection", function(e) {
>                         myObjectArray[19].Wgt.setBackgroundColor("blue");
>  //  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>                 }); // end addListener
>
> This is a simplified example, and my actual code is much longer. Which is
> why I would like to do this automated in a loop instead of adding the
> almost same codeblock 20 times.
>
> There are most probably ways to do it, but my JavaScript experience never
> had to deal with this before. So much not, that I don't even know how to
> name this behaviour and how to search for a solution on-line. I tried
> searching for "javascript variable unreferencing", "javascript variable
> dereferencing", even tried solving it with "eval(ri)" as the array index.
> No solution so far.
>
> I am happy to RTFM, just give me a pointer to the right direction, please!
>
> Regards,
> Keve Nagy * Debrecen * Hungary
>
------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121054471&iu=/4140/ostg.clktrk
_______________________________________________
qooxdoo-devel mailing list
qooxdoo-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to