使用 e4x 在 Flex 中编辑 XML

发布于 2024-07-04 20:24:19 字数 855 浏览 8 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

|煩躁 2024-07-11 20:24:23

不幸的是,函数闭包在 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.

浮世清欢 2024-07-11 20:24:21

闭包仅捕获对变量的引用,而不是其当前值。 由于局部变量是函数范围的(而不是块范围的),循环中的每次迭代都会创建一个捕获对同一变量的引用的闭包。

您可以将 TextInput 创建代码提取到一个单独的函数中,这将为您提供一个单独的变量实例来捕获闭包。 像这样的事情:

for each (var node:XML in xml.node)
{
    var textInput:TextInput = createTextInput(node);
    this.addChild(pileHeightEditor);
}
... 

private function createTextInput(node:XML) : TextInput {
    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);
    return 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:

for each (var node:XML in xml.node)
{
    var textInput:TextInput = createTextInput(node);
    this.addChild(pileHeightEditor);
}
... 

private function createTextInput(node:XML) : TextInput {
    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);
    return textInput;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文