flex在mxmls之间传递数据

时间:2011-07-17 08:37:19

标签: flex

index.mxml:

 <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" horizontalScrollPolicy="auto" verticalScrollPolicy="auto" xmlns:s="library://ns.adobe.com/flex/spark"
                initialize="{newfile.send()}" xmlns:local="*">
    <mx:Style source="CSS/goodlist.css"/>
    <mx:Script>
        <![CDATA[

            import com.adobe.serialization.json.*;

            import mx.collections.ArrayCollection;

            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.utils.URLUtil;

            public var jsonStr:String;
            public var jsonArr:Array;


            [Bindable]
            private var catalog:ArrayCollection;
            protected function newfile_resultHandler(event:ResultEvent):void
            {
                jsonStr = String(newfile.lastResult);
                jsonArr = JSON.decode(jsonStr) as Array;

                var products:ArrayCollection = new ArrayCollection(jsonArr);
                var temp:ArrayCollection = new ArrayCollection();
                var product:Product;


                for each(var pro:Object in products)
                {
                    product = new Product();
                    product.name = pro.name;
                    product.ID = pro.ID;
                    product.category = pro.category;
                    product.description = pro.description;
                    product.price = pro.price;
                    product.unitID =pro.unitID;
                    product.cutoff = pro.cutoff;
                    product.store = pro.store;
                    product.locationID = pro.locationID;

                    temp.addItem(product);
                }

                catalog = temp;

                show.text = String(catalog.length);

                saleStack._catalog = catalog
            }
        ]]>
    </mx:Script>
    <mx:HTTPService url="http://localhost/newfile.php" id="newfile"
                    result="newfile_resultHandler(event)"
                    method="GET" resultFormat="text">
    </mx:HTTPService>
    <mx:VBox>
        <local:saleView width="100" height="100" id="saleStack" showEffect="WipeDown" hideEffect="WipeUp"/>
        <mx:Label id="show"/>
    </mx:VBox>
</mx:Application>

saleView.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas 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="400" height="300"
         initialize="{this.canvas1_initializeHandler()}">

    <fx:Declarations>

    </fx:Declarations>


    <fx:Script>
        <![CDATA[
            import CatalogEvent;
            import Product;


            import flash.utils.Dictionary;

            import mx.collections.ArrayCollection;


            import mx.events.FlexEvent;


            private var productCatalog:Array;


            [Bindable]
            public var _catalog:ArrayCollection;


            protected function canvas1_initializeHandler():void
            {
                test.text = String(_catalog);

            }

        ]]>
    </fx:Script>

    <mx:Label id="test"/>
</mx:Canvas>

然后test.text显示“null”

但是如果删除test.text = String(_catalog); 并像这样更改标签测试

<mx:Label id="test" text="{_catalog}"/>

测试将显示_catalog

我不知道,我想在许多其他功能中使用_catalog

请.....

2 个答案:

答案 0 :(得分:0)

在使用newfile_resultHandler设置_catalog之前,

canvas1_initializeHandler被调用一次,因此它将为null。

您的其他示例使用数据绑定在其依赖项更改时更新属性。要在ActionScript中获取此功能,请使用ChangeWatcher或BindingUtils:Defining data bindings in ActionScript,或者可以为处理更新的目录创建set函数。

答案 1 :(得分:0)

You need to use data binding in mentioned example. The changes need to be done in given example is mentioned below.

1. Change
[Bindable]
private var catalog:ArrayCollection;

To 

[Bindable]
public var catalog:ArrayCollection;

2. Change 
 <local:saleView width="100" height="100" id="saleStack"  showEffect="WipeDown" hideEffect="WipeUp"/>

To

<local:saleView width="100" height="100" id="saleStack" _catalog="{catalog}" showEffect="WipeDown" hideEffect="WipeUp"/>

3. Use proper data binding to bind data , that has to be shown in Lable or else where , because in  given example binding will not happen when http call fetches data from server .You need to declare bindable variable and bind it properly with lable. some of this shot

 [Bindable]
 public var _catalog:ArrayCollection;
 [Bindable]
 private var tempStr:String;


            protected function canvas1_initializeHandler():void
            {
                tempStr = String(_catalog);

            }

<mx:Label id="test" text="{tempStr}"/>