гибкий проход данных между mxmls - PullRequest
0 голосов
/ 17 июля 2011

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 голосов
/ 17 июля 2011
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}"/>
0 голосов
/ 17 июля 2011

canvas1_initializeHandler вызывается один раз, до того как _catalog был установлен newfile_resultHandler, поэтому он будет нулевым.

Ваш другой пример использует привязку данных для обновления свойств при изменении их зависимостей. Чтобы получить это в ActionScript, используйте ChangeWatcher или BindingUtils: Определение привязок данных в ActionScript или вы можете создать функцию набора для каталога, которая обрабатывает обновления.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...