Fix closure problem when importing multiple files
authorRichard Fairhurst <richard@systemeD.net>
Thu, 9 Jun 2011 17:28:58 +0000 (18:28 +0100)
committerRichard Fairhurst <richard@systemeD.net>
Thu, 9 Jun 2011 17:28:58 +0000 (18:28 +0100)
REFACTOR.txt
net/systemeD/potlatch2/utils/Importer.as

index dc76e3de668245bb731c147bfa6e7ae8dd84fb1c..fd67b90252ea8415a609ffd85d24dfc1743952c7 100644 (file)
@@ -4,12 +4,6 @@ Aims:
 - to enable multiple Maps to be shown on-screen (e.g. for junction editor)
 - to bring Connection and VectorLayer code together
 
-To check:
-- MapDust
-- VectorLayers from GPX
-- VectorLayers from shapefiles
-- removeLayer in MapPaint.as should blank - does it?
-
 == Changes overall ==
 
 Map.as becomes a collection of MapPaint objects, all with the same lat/long/scale
index c664530dfcd25c5d2dc18768758f4709533c7450..e53355de6a04ae49d29c4f229b70642d90c55e8c 100644 (file)
@@ -27,21 +27,19 @@ package net.systemeD.potlatch2.utils {
                        this.callback=callback;
                        this.simplify=simplify;
 
-                       var sp:uint=0;
-                       for each (var fn:String in filenames) {
-                               var thissp:uint=sp;             // scope within this block for the URLLoader 'complete' closure
-                               trace("requesting file "+fn);
+                       // Use forEach to avoid closure problem (http://stackoverflow.com/questions/422784/how-to-fix-closure-problem-in-actionscript-3-as3#3971784)
+                       filenames.forEach(function(fn:String, index:int, array:Array):void {
+                               trace("requesting file "+index);
                                var request:DebugURLRequest = new DebugURLRequest(fn);
                                var loader:URLLoader = new URLLoader();
                                loader.dataFormat=URLLoaderDataFormat.BINARY;
-                               loader.addEventListener(Event.COMPLETE,function(e:Event):void { fileLoaded(e,thissp); });
+                               loader.addEventListener(Event.COMPLETE,function(e:Event):void { fileLoaded(e,index); });
                                if (callback!=null) {
                                        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler);
                                        loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler);
                                }
                                loader.load(request.request);
-                               sp++;
-                       }
+                       });
                }
                
                protected function fileLoaded(e:Event,filenum:uint):void {