我正在使用Zend AMF从PHP应用程序中获取一些数据。但是我无法将数据绑定到简单的DropDownList控件。 PHP方法是:
class Test
{
public function myMethod()
{
$res = array();
$res[] = array('NAME' => 'ThisIsATest', 'ID' => 1);
return $res;
}
}
网络监视器报告该方法返回结果。它将以下数据作为数组返回:
Array
(
[0] => Array
(
[NAME] => Property
[ID] => 1
)
)
以下是Flex代码:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="500" height="286"
creationComplete="initApp()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private function myMethodResult(e:ResultEvent):void
{
searchType.dataProvider = e.result as ArrayCollection;
}
protected function initApp():void
{
service.myMethod();
}
protected function faultHandler(event:FaultEvent):void
{
trace(event.fault.faultString);
}
]]>
</fx:Script>
<fx:Declarations>
<s:RemoteObject id="service"
destination="zend"
source="Test"
showBusyCursor="true"
fault="faultHandler(event)">
<s:method name="myMethod" result="myMethodResult(event)"/>
</s:RemoteObject>
</fx:Declarations>
<s:DropDownList id="searchType" labelField="NAME"/>
</s:WindowedApplication>
非常感谢任何帮助。提前谢谢。
答案 0 :(得分:4)
你问约束力,但我认为这不是你想知道的。我相信答案是结果处理程序中的这一行:
searchType.dataProvider = e.result as ArrayCollection;
我假设您从ColdFusion获取一个数组。如果内存为我服务,则不能将数组转换为ArrayCollection。结果很可能是空的。您是否在调试模式中逐步执行代码以验证?
而是试试这个:
searchType.dataProvider = new ArrayCollecection(e.result as Array);
由于e.result是一个通用对象,因此您需要将其转换为数组。
解决答案的绑定部分。绑定具有源和值。当源更改时,该值将自动更新。您有一个要更改的值(dropDownList.dataProvider),但您没有该源。代码中没有任何内容可以使用绑定。您只需在结果返回时手动设置值。要使用绑定,我可能会像这样修改你的代码:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="500" height="286"
creationComplete="initApp()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
// create a variable taht can be used as the source for a binding operation
[Bindable]
public var mySource : ArrayCollection;
private function myMethodResult(e:ResultEvent):void
{
// searchType.dataProvider = e.result as ArrayCollection;
// change the value of your binding source
mySource = new ArrayCollection(e.result);
}
protected function initApp():void
{
service.myMethod();
}
protected function faultHandler(event:FaultEvent):void
{
trace(event.fault.faultString);
}
]]>
</fx:Script>
<fx:Declarations>
<s:RemoteObject id="service"
destination="zend"
source="Test"
showBusyCursor="true"
fault="faultHandler(event)">
<s:method name="myMethod" result="myMethodResult(event)"/>
</s:RemoteObject>
</fx:Declarations>
<!-- and finally, specify your dataProvider as the target for binding -->
<s:DropDownList id="searchType" labelField="NAME" dataProvider="{this.mySource }"/>
</s:WindowedApplication>
我在浏览器中编写了所有代码,它可能不是“编译完美”
答案 1 :(得分:0)
@Flextras
searchType.dataProvider = new ArrayCollecection(e.result);
...导致......
1118: Implicit coercion of a value with static type Object to a possibly unrelated type Array.
相反,我试过......
searchType = ArrayCollection(e.result);
但这导致......
Error #1034: Type Coercion failed: cannot convert []@812a1c9 to mx.collections.ArrayCollection
然后我试了......
typeArray.source = e.result as Array;
...和...
<s:DropDownList labelField="NAME">
<s:ArrayCollection id="typeArray"/>
</s:DropDownList>
这个有效! \ O /