title: Editing XML in Flex using e4x
body: <p>In Flex, I have an xml document such as the following:</p>
  
  <pre><code>var xml:XML = &lt;root&gt;&lt;node&gt;value1&lt;/node&gt;&lt;node&gt;value2&lt;/node&gt;&lt;node&gt;value3&lt;/node&gt;&lt;/root&gt;
  </code></pre>
  
  <p>At runtime, I want to create a TextInput control for each node under root, and have the values bound to the values in the XML. As far as I can tell I can't use BindingUtils to bind to e4x nodes at runtime (please tell me if I'm wrong here!), so I'm trying to do this by hand:</p>
  
  <pre><code>for each (var node:XML in xml.node)
  {
      var textInput:TextInput = new TextInput();
      var handler:Function = function(event:Event):void 
      {
          node.setChildren(event.target.text);
      };
      textInput.text = node.text();
      textInput.addEventListener(Event.CHANGE, handler);
      this.addChild(pileHeightEditor);
  }
  </code></pre>
  
  <p>My problem is that when the user edits one of the TextInputs, the node getting assigned to is always the last one encountered in the for loop. I am used to this pattern from C#, where each time an anonymous function is created, a "snapshot" of the values of the used values is taken, so "node" would be different in each handler function.</p>
  
  <p>How do I "take a snapshot" of the current value of node to use in the handler? Or should I be using a different pattern in Flex?</p>