Apache flex 如何将XMLListCollection绑定到模型中?

Apache flex 如何将XMLListCollection绑定到模型中?,apache-flex,actionscript,datagrid,model,mxml,Apache Flex,Actionscript,Datagrid,Model,Mxml,在DataGrid中更新XMLListCollection时,我希望它将XML(绑定)添加到模型中。我该怎么做?如果不可能,我将如何手动将XML添加到模型中 这是我试图做的一个粗略的例子。向DataGrid添加数据效果良好;它只是在发送到HTTPService时不进入模型 <mx:Model id="model"> <root><colors>{collection}</colors></root> </mx:Model>

在DataGrid中更新XMLListCollection时,我希望它将XML(绑定)添加到模型中。我该怎么做?如果不可能,我将如何手动将XML添加到模型中

这是我试图做的一个粗略的例子。向DataGrid添加数据效果良好;它只是在发送到HTTPService时不进入模型

<mx:Model id="model">
  <root><colors>{collection}</colors></root>
</mx:Model>
<mx:XMLListCollection id="collection" />

<mx:Label text="Input color:" />
<mx:TextInput id="color" />
<mx:Button label="Add Color" click="addColor();" />
<mx:DataGrid dataProvider="{collection}">
  <mx:columns>
    <mx:DataGridColumn dataField="color" headerText="Color" />
  </mx:columns>
</mx:DataGrid>
<mx:Button label="Submit" click="submit();" />

<mx:Script><![CDATA[
  // imports...

  public function addColor():void {
    var xml:XML = new XML(<color></color>);
    xml.color = color.text;
    collection.addItem(xml);
    Alert.show(collection.toXMLString()); // xml looks fine and shows up in datagrid
    // how to do something like this?
    // model.appendChild(xml); // doesn't work
  }

  public function submit():void {
    var serv:HTTPService = new HTTPService();
    // setup serv...
    serv.send(model); // server just gets "<colors></colors>"
  }
]]></mx:Script>

{collection}
);
xml.color=color.text;
collection.addItem(xml);
Alert.show(collection.toXMLString());//xml看起来不错,并显示在datagrid中
//怎么做这样的事?
//model.appendChild(xml);//不起作用
}
公共函数submit():void{
var serv:HTTPService=newhttpservice();
//安装服务。。。
serv.send(model);//服务器刚刚获取“”
}
]]>
谢谢



编辑: 谢谢你们的回答!这最终导致了我的解决方案。我不得不使用XMLListCollection,因为无论出于何种原因,向DataGrid添加2个或更多元素都无法使用XML或XMLList(我尝试了{variable}、{variable.colors}、{variable.colors.color}、{variable.color}、{variable.color}等等,所有的可能性)。我认为我可以使用一个模型,但我在下面将其更改为XML,以便您可以使用toXMLString()查看输出。最后,我必须使用模型或XML,因为这只是大型项目的一个小例子(我通过在下面添加其他数据来说明这一点)。再次感谢。下面是一个工作示例:

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

  <mx:XML id="data">
    <colors>
    </colors>
  </mx:XML>

  <mx:XMLListCollection id="collection" source="{data.color}" />

  <mx:XML id="model">
    <root>
      <otherData>{otherData.text}</otherData>
      <colors>{data}</colors>
    </root>
  </mx:XML>

  <mx:Label text="Input color:" />
  <mx:TextInput id="color" />
  <mx:Button label="Add Color" click="addColor();" />
  <mx:DataGrid dataProvider="{collection}">
    <mx:columns>
      <mx:DataGridColumn dataField="color" headerText="Color" />
    </mx:columns>
  </mx:DataGrid>
  <mx:Label text="Other data:" />
  <mx:TextInput id="otherData" />
  <mx:Button label="Submit" click="submit();" />

  <mx:Script><![CDATA[
    import mx.controls.*;

    public function addColor():void {
      var xml:XML = new XML(<color></color>);
      xml.color = color.text;
      collection.addItem(xml);
      Alert.show(collection.toXMLString()); // xml looks fine and shows up in datagrid
    }

    public function submit():void {
      Alert.show(model.toXMLString());
    }
  ]]></mx:Script>

</mx:Application>

{otherData.text}
{data}
);
xml.color=color.text;
collection.addItem(xml);
Alert.show(collection.toXMLString());//xml看起来不错,并显示在datagrid中
}
公共函数submit():void{
Alert.show(model.toXMLString());
}
]]>

以下是您的一些误解:

<root><colors>{collection}</colors></root>
应该是:

var xml:XML = <color><value/></color>;
var-xml:xml=;
XML在AS3中有特殊的文字语法,您用来创建新XML的已经是XML了,所以本质上,您做了两次。没有文本值的节点被规范化为更短,因此您尝试以您这样做的方式写入它们将没有效果


XMLListCollection
实际上是一个无用的类,不知道它为什么会存在。您可以指定
XML
XMLList
作为
DataGrid
的数据提供程序
Model
是另一个无用的类,因此,可能您可以将其释放并替换为
dataGrid.dataProvider
,您需要在其中引用dataGrid的内容。

我不确定您为什么说XMLListCollection是useles…它允许XML用作数据提供程序并生成所有更改事件,等等,类似于ArrayCollection允许将数组绑定为数据提供程序,并在添加和删除项时更新列表。这里无用的类是Model。我看不出这有什么意义(也从未找到过它的用例)。在这种情况下,为什么不只是server.send({collection.source as XMLList});?这假设服务器实际上需要所有额外的嵌套,而不仅仅是将其剥离……否则,您可以只使用server.send(collection.source);谢谢,请看我在问题底部的编辑;我使用的是v4.1,我的代码运行正常。另外,我使用的是免费的mxmlc编译器,而不是FlashBuilder。也许它在高于4.1的版本中被破坏了?
var xml:XML = <color><value/></color>;