Hi C.

thanks for sharing this 'trick'. I don't know whether I will need it in the
future, but I definitely would have needed it in the past ;) Unfortunately I
didn't knew that trick back then.

However the _most_important_part_ is the *comment*, describing _why_ the coder
did this unusual thing... ;)

Cheers, and thanks for the info,
  Peter

On 2010-09-15 10:19 panyasan wrote:
> Hi list,
> 
> I have often run into the problem that variables used in a loop wouldn't be
> passed correctly into an event listener because of a known scoping problem
> in JavaScript that will be solved by the "let" keyword in ECMAScript 5. The
> problem will be known to many: variables in a loop passed to a closure
> function will always contain the *last* value assigned to it, not the value
> it had when the event listener war defined. I finally found a simple but
> effective solution described here:
> 
> http://www.mennovanslooten.nl/blog/post/62
> 
> This might be no big news for many of you, but maybe it helps some others
> like me. It solves, for example, this case:
> 
>       for( var i=0; i< labels.length; i++) 
>       {
>         var label = new qx.ui.basic.Label(labels[i]);
>         this.addListener("my-special-event",function(row,label){ 
>           return function(e){
>                 if( e.getData() == row ) {
>                   label.setBackgroundColor( "white" );  
>                 }
>                 else {
>                   label.resetBackgroundColor();
>                 }
>               }
>         }(i,label)); // to get the variables into the closure
>         grid.add( label, { row : i, column : 0 } );
>       }
> 
> Cheers,
> C. 

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
qooxdoo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to