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. -- View this message in context: http://qooxdoo.678.n2.nabble.com/loops-event-listeners-and-closures-tp5531147p5531147.html Sent from the qooxdoo mailing list archive at Nabble.com. ------------------------------------------------------------------------------ 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
