Posts in Category "Flex 2"

Example of Designing Loosely Coupled Flex 2 Components — pass data from main to child component

There is a good article regarding Designing Loosely Coupled Flex 2 Components. This is a good place to learn about different ways to design your components in Flex 2. On the third page of this article, it demonstrated how to Create Custom Event ActionScript Classes for sending complex data with an example. This example shows how to send data to the parent page by dispatch an event from the child component. What if you want to dispatch the event from the parent page, and pass the data into the child? Here is a simple example to show how we can accomplish that.

1. create a customer event ActionScript class:

In order to pass extra data via event, a customer event class is needed. We do this by extends existing Flex event classes and inherit all the events of the base class.  Here we create to pass selectedAlbum with the event.

     import mx.core.Application;

    public class myEvent extends Event
            public var myAlbum:String;
            public function myEvent(selectedAlbum:String,type:String) {
                     this.myAlbum = selectedAlbum;
           public override function clone():Event{
                    return new myEvent(myAlbum,type);

2.  Create the main application:

The main application has a datagrid. The goal is to be able to display data in a new state based on selected row in the datagrid. The follwoing two handlers are the most improtant:

1).datagrid’s itemClick event handler itemClickEvent():  this will get the selected row and value of selectedAlbum.

2).eventHandler(): this will display the new state, dispatch event myEvent (must make sure the event is dispatched after the child is created), and pass selectedAlbum to the child component. We reference the child component as <v:looselyCoupledChild />.

Note, eventObj = new myEvent(selectedAlbum,"passID");

Here, passID is the event name we will define in the Metadata of the child component:

[Event(name="passID", type="myEvent")]

main flex page looselyCoupled.mxml:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="" xmlns:v="*" layout="vertical">

<!–This example passes selectedAlbum to child.mxml by listening  to myEvent –>        





                      import mx.collections.ArrayCollection;


                      public var selectedAlbum:String;

                      public var eventObj:myEvent;


                      private var initDG:ArrayCollection = new ArrayCollection([

            {Artist:’Pavement’, Album:’Slanted and Enchanted’, 

                      Price:11.99, SalePrice: true },

            {Artist:’Pavement’, Album:’Brighten the Corners’,   

                      Price:11.99, SalePrice: false }


        private function itemClickEvent(event:ListEvent):void {


                                    eventObj = new myEvent(selectedAlbum,"passID");




                             private function eventHandler(stateView:String):void{

                             //the stateView must be created before the event dispatch.

                                    this.currentState = stateView;



                      private function dispatchEvt():void{

                                    eventObj = new myEvent(selectedAlbum,"passID");





<mx:Label id="lb"  text="show selectedAlbum — from myEvent"/>

<mx:Label id="lb1"  text="show selectedAlbum — from eventObj.myAlbum"/>

<mx:DataGrid  id="dg" dataProvider="{initDG}"  itemClick="itemClickEvent(event)">


<mx:Button x="117" y="240" label="dispaly Album"  id="bt" click="eventHandler(‘child’)" />


                        <mx:State name="child">

                                      <mx:AddChild position="lastChild">

                                                                <v:looselyCoupledChild />





3. create the child component:

In the child component, we need to add event listener to listen to passID as following:   Application.application.addEventListener("passID", passIDHandler);


<?xml version="1.0" encoding="utf-8"?>
<mx:Panel xmlns:mx="" width="400" height="300" initialize="initHandler()" >
[Event(name="passID", type="myEvent")]

import mx.core.Application;
import myEvent;
private function initHandler():void{
Application.application.addEventListener("passID", passIDHandler);

private function passIDHandler(event:myEvent):void {
tinput.text = event.myAlbum;


<mx:Label text="Album you selected from event:" x="26" y="10"/>
<mx:TextInput x="107" y="8" id="tinput" />
<mx:Label x="291" y="10" text="Album you selected from selectedAlbum:"/>
<mx:TextInput x="372" y="8" text="{Application.application.selectedAlbum}"/>


Note, Application.application.addEventListener("passID", passIDHandler);
  This means we will always have a reference to the child. If you are concerned about memory issue, you should check and destroy the event when it is done. If you don’t want to destroy the event, you can set weakRef to true in addEventListener.

To test the sample,

1). run, and select a row. You will see the Album name displayed on top of the datagrid.

2). click on "dispaly Album" Button, you will see the Album name has passed into the child component.

3). select another row in the datagrid, you will see the value in the child component is changed as well.

Links to nice flex 2 examples

1. Design loosely coupled Flex 2 components

2. Export datagrid to Excel:
If manfully copy from a datagrid and paste to Excel is sufficient , see:
If you need the application to generate the excel for you , see:

3.Internationalizing Flex Apps

4. Coloring the Background of Cells

Scaling an image with smoothing

6. Unicode support
In order for it to work, you have to make sure
1).your font must support those characters.
2). you need to set the correct fontFamily,
3). you need to set character ranges

Here are couple of simple examples: to come ….

How to access cfc on your Coldfusion server from flex app on a separate server.

Coldfusion 7.0.2 added flex feature support, hence you can access your cfc service from flex app. See a good article by Kyle about how this works at here.

The above article described how to access cfc using RemoteObject in flex app when your cfc and mxml pages are both on the same Coldfusion server. If you have your cfc services in an existing Coldfusion app, and you want to create new flex app on a remote server, how do you access the existing cfc services now? Here we will go through the steps to make that configuration work.

The reason the example works in above article is because CF 7.0.2 implemented gateway to access AMF, and by default a destination/channel is defined in CF app WEB-INF/flex/services-config.xml. When running the flex app on a remote server, we will need to use flex app gateway instead, and configure destination/channel in the flex app.

Let us use the example in the above article, download to your machine.

1. Extract HelloWorld.cfc to your Coldfusion app root. For me, it is C:\flex\jrun4\servers\cfusion\cf702. Here, cfusion is a server instance, /cf702 is the context root. It uses port 8300.

2. Extract folder HelloWorld_ro to your flex app root. For me, it is C:\flex\jrun4\servers\default\samples2, default is a server instance, /samples2 is the context root. It uses port 8700.

3. Copy the following and Paste it into your flex app WEB-INF/flex/remoting-config.xml, inside the <service> tag, parallel to other destination tag.

<destination id=”ColdFusion”><channels> <channel ref=”my-cfamf”/> </channels> <properties> <source>*</source> </properties> </destination>

4. Copy the following and Paste into your flex app\’s WEB-INF/flex/services-config.xml, inside <channels> tag, parallel to other channel-definition.

<channel-definition id=”my-cfamf” class=”mx.messaging.channels.AMFChannel”> <endpoint uri=”http://{}:{server.port}{context.root}/flex2gateway/” class=”flex.messaging.endpoints.AMFEndpoint”/> <properties> <polling-enabled>false</polling-enabled> <serialization> <instantiate-types>false</instantiate-types> </serialization> </properties> </channel-definition>

5. Define your endpoint. You can configure the destination in servicees-config.xml. See above channel-definition you just added. You can change uri=”http://{}:{server.port}{context.root}/flex2gateway/” to point to your real server name and port. For this example, it is http://linIp:8300/cf702/flex2gateway.

Or you can override it in your RemoteObject tag:

<mx:RemoteObject endpoint=”http://linIp:8300/cf702/flex2gateway/”
source=”HelloWorld”><mx:method name=”sayHelloString” result=”handleStringResult(event)” fault=”” /></mx:RemoteObject>

6. Make sure both Coldfusion and flex server are running.

7. run HelloWorld_ro.mxml either from flex builder or by request
http://localhost:8700/samples/HelloWorld_ro/ HelloWorld_ro.mxml,
you should see a button with label "get String Remote Object", click on it, you should see "Hello World!" displayed on top of the button.

8. If you are using standalone Coldfusion with default install, then put HelloWorld.cfc under C:\CFusionMX7\wwwroot. Note the context root now is /. If you are using port 8500 for this server, then your endpoint would be:

Note, step 3 to step 6 is to configure channel in the configuration files. You can also assign channels on the client at run time by creating a ChannelSet object that contains one or more Channel objects, and assigning the ChannelSet to a service componen. See flex doc for details.