1

我正在使用 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
        )
)

下面是弹性代码:

<?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>

任何帮助将不胜感激。提前致谢。

4

2 回答 2

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>

我在浏览器中编写了所有代码,它可能不是“编译完美”

于 2010-09-08T15:43:34.903 回答
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/

于 2010-09-08T16:28:44.710 回答