开发者

How to send an ArrayCollection to a new/sub-window?

开发者 https://www.devze.com 2023-03-16 20:08 出处:网络
Yesterday phtrivier showed me how to send an array to a new/sub-window. Now I have replaced this static source of data with an XML file that loads into an ArrayCollection. Unfortunately I found an Ar

Yesterday phtrivier showed me how to send an array to a new/sub-window.

Now I have replaced this static source of data with an XML file that loads into an ArrayCollection. Unfortunately I found an ArrayCollection behaves differently than an Array when you try to send a part of it to a new/sub-window.

How can I do this with the ArrayCollection?

Or should I take the easy road with sending an Array and instead look for a way to make the XML load into an Array instead of an ArrayCollection? I don't think I will require the extra features an AC offers.

MyMain.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication ...stuff... creationComplete="settingService.send()">
    <fx:Declarations>
        <s:HTTPService id="settingService" url="data.xml" result="settingService_resultHandler(event)"/>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            // import dependencies
            import mx.collections.ArrayCollection;
            import mx.rpc.events.ResultEvent;

            // variables
            [Bindable] private var xmlData:ArrayCollection;

            // collect static data
            private var staticData1:Array = new Array('The Eiffel Tower','Paris','John Doe');
            private var staticData2:Array = new Array('The Strip','Las Vegas','Jane Doe');
            private var staticData:Array = new Array(staticData1, staticData2);

            // collect xml data
            protected function settingService_resultHandler(event:ResultEvent):void
            {
                xmlData = event.result.settings.photo;
            }

            // open window & send data in Array, WORKING
            public function openWin1(inData:Array):void
            {
                var w:MyWindow1 = new MyWindow1();
                w.inData = inData;
                w.open();
            }

            // open window & send data in ArrayCollection, NOT WORKING
            public function openWin2(inData:ArrayCollection):void
            {
                var w:MyWindow2 = new MyWindow2();
                w.inData = inData;
                w.open();
            }
        ]]>
    </fx:Script>
    <!--opening windows, adding an array, WORKING-->
    <s:Button x="10" y="10" width="240" label="open a sub-window 1" click="openWin1(staticData[0]);"/>
    <s:Button x="10" y="30" width="240" label="open a sub-window 2" click="openWin1(staticData[1]);"/>
    <!--opening windows, adding an arraycollection, NOT WORKING-->
    <s:Button x="10" y="60" width="240" label="open a sub-window 1" click="openWin2(xmlData.getItemAt(5));"/>
    <s:Button x="10" y="80" width="240" label="open a sub-window 2" click="openWin2(xmlData[5].source);"/>
    <s:Button x="10" y="100" width="240" label="open a sub-window 3" click="openWin2(xmlData.getItemAt(5).source);"/>
</s:WindowedApplication>

MyWindow1.mxml (should be fine, its working after all)

<?xml version="1.0" encoding="utf-8"?>
<mx:Window ...stuff...>
    <mx:Script>
        <![CDATA[
            // variables
            [Bindable] private var windowData:Array;

            // receive data
            public function set inData(outData:Array):void {
                this.windowData = outData;
            }
        ]]>
    </mx:Script>
    <mx:TextInput id="comment" x="10" y="10" text="{windowData[0]}"/>
    <mx:TextInput id="location" x="10" y="30" text="{windowData[1]}"/>
    <mx:TextInput id="author" x="10" y="50" text="{windowData[2]}"/>
</mx:Window>

MyWindow2.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Window ...stuff...>
    <mx:Script>
        <![CDATA[
            // import dependencies
            import mx.collections.ArrayCollection;

            // variables
            [Bindable] private var windowData:ArrayCollection;

            // receive data
            public function set inData(outData:ArrayCollection):void {
                this.windowData = outData;
            }
        ]]>
    </mx:Script>
    <mx:TextInput id="comment" x="10" y="10" text="{windowData.comment}"/>
    <mx:TextInput id="locatio开发者_运维知识库n" x="10" y="30" text="{windowData.location}"/>
    <mx:TextInput id="author" x="10" y="50" text="{windowData.author}"/>
</mx:Window>


Found my own solution: The XML loaded comes in as ArrayCollection but I needed to run it through a repeater anyway. When ran through a repeater the arrays inside become an Object. I have just as much experience with Objects as ArrayCollections BUT by trial & error I found the following working code. Yay!

// MyMain.mxml // inside the repeater
click="openWin(event.currentTarget.getRepeaterItem())"

// MyMain.mxml // opening the window and sending the data
private function openWin(transferData:Object):void
{
    var w:MyWindow = new MyWindow();
    w.transferData = transferData;
    w.open();
}

// MyWindow.mxml // variables
[Bindable] private var windowData:Object;

// MyWindow.mxml // receive the data
public function set transferData(transferData:Object):void
{
    this.windowData = transferData;
}

// MyWindow.mxml // use the data
<mx:Label text="{windowData.author}"/>

While this isn't sending an ArrayCollection, it solved my problem and may make sending an actual ArrayCollection very easy.

0

精彩评论

暂无评论...
验证码 换一张
取 消