How to create flex component and best practices

Saturday, October 30, 2010 Posted by Sanka Senevirathna 0 comments
Intercommunication with flex components


Application Flow

App.mxml à Component .mxml

Data passing

Component àCustomEvent Object à Application

Why we say loosly couple?

Component properties aren’t accessed directly by Application.Otherwose when component change some id property or something, we have to update all the refered applications.



app.mxml is the main application.GetKey.mxml is the component and GetKeyEvent.as is the custom event class that transfer the data.

There is a variable in GetKeyEvent.as and that is public as you can see.This is the one that access both GetKey.mxml component and app.mxml application.

[Bindable]

public var key:String=null;

GetKey.mxml push the data to key and app.mxml read the data from key in GetKeyEvent.as.

GetKey.mxml push the data to key

var getKeyEvent:GetKeyEvent= new GetKeyEvent("getKey",txtKey.text);

getKeyEvent.key=txtKey.text;

dispatchEvent(getKeyEvent);

app.mxml read the data from key in GetKeyEvent.as

var getKey:GetKey =new GetKey();

getKey.addEventListener("getKey",getkey1_getKeyHandler);

How to registering an event

As following we have to register an event in our component.getKey is the name of event that visible to main app.

[Event(name="getKey",type="events.GetKeyEvent")]

How to Access the component and it’s events from main app

You can download the sample project from here

http://dl.dropbox.com/u/7375335/components.fxp

Labels: