使用 e4x 在 Flex 中编辑 XML
在 Flex 中,我有一个如下所示的 xml 文档:
var xml:XML = <root><node>value1</node><node>value2</node><node>value3</node></root>
在运行时,我想为 root 下的每个节点创建一个 TextInput 控件,并将值绑定到 XML 中的值。 据我所知,我无法在运行时使用 BindingUtils 绑定到 e4x 节点(如果我错了,请告诉我!),所以我尝试手动执行此操作:
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);
}
我的问题是,当用户编辑 TextInput 之一时,分配到的节点始终是 for 循环中遇到的最后一个节点。 我习惯了 C# 中的这种模式,每次创建匿名函数时,都会获取所用值的“快照”,因此每个处理函数中的“节点”都会不同。
如何“拍摄”节点当前值的快照以在处理程序中使用? 或者我应该在 Flex 中使用不同的模式?
In Flex, I have an xml document such as the following:
var xml:XML = <root><node>value1</node><node>value2</node><node>value3</node></root>
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:
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);
}
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,函数闭包在 Actionscript 中工作得很奇怪/很糟糕。 变量只有在超出范围时才会获得“快照”。 不幸的是,变量是函数作用域的,而不是块作用域的。 所以它最终不会像你想要的那样工作。
您可以创建一个字典来从 TextInput -> 进行映射 节点,或者您可以将节点存储在
TextInput 的
数据属性中。我希望你所描述的能够正常工作,因为这是一种简单/强大的表达方式。
Unfortunately, function closures work weird/poorly in Actionscript. Variables only get a "snapshot" when they go out of scope. Unfortunately, variables are function scoped, and not block scoped. So it doesn't end up working like you want.
You could create a dictionary to map from TextInput -> node, or you could stash the node in the
TextInput's
data property.I wish what you described did work correctly since it is an easy/powerful way of expressing that.
闭包仅捕获对变量的引用,而不是其当前值。 由于局部变量是函数范围的(而不是块范围的),循环中的每次迭代都会创建一个捕获对同一变量的引用的闭包。
您可以将 TextInput 创建代码提取到一个单独的函数中,这将为您提供一个单独的变量实例来捕获闭包。 像这样的事情:
The closure only captures a reference to the variable, not its current value. Since local variables are Function-scoped (not block-scoped) each iteration through the loop creates a closure that captures a reference to the same variable.
You could extract the TextInput creation code into a separate function, which would give you a separate variable instance to capture for the closure. Something like this: