Passing data between ViewNavigators in Flash Builder 4.5 Burrito

FB 4.5 Burrito is an awesome tool that is still on the works. While playing with it I noticed that passing data between sub-views was a pretty straight forward implementation. Passing data between application root views was another issue. Handling the application CustomEvent.DATA does the job.

The main application

<s:TabbedMobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="init()">
<fx:Declarations>
</fx:Declarations>

<fx:script><![CDATA[
private function init():void{
nav1.addEvenListener(CustomEvent.DATA, onData);
}

private function onData(ev:CustomEvent):void{
nav2.setData(ev.data);
}
]]></fx:script>

<s:ViewNavigator id="nav1" label="Nav1" firstView="views.Nav1Home" width="100%" height="100%"/>
<s:ViewNavigator id="nav2" label="Nav2" firstView="views.Nav2Home" width="100%" height="100%"/>

</s:TabbedMobileApplication>

CustomEvent.as

import flash.events.Event;

public class CustomEvent extends Event
{
public static var DATA:String = "DATA_EVENT";
public var data:Object = null;

public function CustomEvent(data:Object, type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.data = data;
}
}

views.Nav1Home.mxml

<?xml version="1.0" encoding="utf-8"?>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
<![CDATA[
private function init(): void {
this.addEventListener(FlexEvent.REMOVING, removedHandler);
}

private function removedHandler(event:Event):void {
trace("Removed from stage: " + data);
this.dispatchEvent(new CustomEvent("Data from Nav1 Event", CustomEvent.DATA));
}
]]>
</fx:Script>

views.Nav2Home.mxml

<?xml version="1.0" encoding="utf-8"?>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
<![CDATA[
private function init(): void {
this.addEventListener(FlexEvent.REMOVING, removedHandler);
}

private function removedHandler(event:Event):void {
trace("Removed from stage: " + data);
this.dispatchEvent(new CustomEvent("Data from Nav2 Event", CustomEvent.DATA));
}
]]>
</fx:Script>

Also, you might want to have a look on Holly Schinsky (AKA DevGirl) blog where she just recently posted two other great options to achieve this same goal. Please refer to Part 1 and Part 2 of her Passing Data Between Tabs post.

Advertisements

About CrazyPenguin

Software Engineer
This entry was posted in FlashBuilder 4.5 (Burrito). Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s