Ahmad Flex

Flex Ideas

Search for component by name in a flex application

Posted by ahmadflex on June 15, 2009


Bookmark and Share

Hi,

You can use below code to search through the entire flex application for a certain UIComponent object by name or ID. It is a recursive function call. (you can use getComponent function which will search the application, or call findComponent function and pass it an array of  components to search in.)

Here is how you call the function:

ComponentFinder.getComponentByName(“addCommentButton”);

 

Here is the code:

////////////////////////////////////////////////////////////////////////////////
//
//  Ahmad Khudairy
//  Copyright 2009 Ahmad Flex
//  All Rights Reserved.
//
//  NOTICE: You can use, and modify this file. Just please
//          Keep this comment undeleted.
//
////////////////////////////////////////////////////////////////////////////////

public class ObjectFinder
{
/**
*
* This will search the application for the component that has a
* certain name or id. and returns the first
* one it finds. otherwise it returns null.
*
* @param componentName String defining component name or id.
*
* @return A refrence to the component found. returns null if not
* found.
*
*/
public static function getComponentByName(componentName:String):UIComponent
{
	//quick return
	if (componentName == "")
	{
		return null;
	}

	var mainApp:Container = Application.application as Container;
	var appChildren:Array = mainApp.getChildren();
	return findComponent(componentName, appChildren);
}

/**
* recursive function. That will loop through an array of components and
* their children components. and it stops if it finds a component with
* name or id equals to parameter componentName.
*
* @param componentName String defining component name or id.
* @param compsArr Array of components to be searched.
*
* @return A refrence to the component found. returns null if not
* found.
*/

public static function findComponent(componentName:String, compsArr:Array):UIComponent
{
	var obj:Object;
	var found:UIComponent;
	var i:int;
	var length:int = compsArr.length;
	var component:Object;

	for (i = 0; i < length ; i++)
	{
		component = compsArr[i];
		if (component.hasOwnProperty("name") && component.name == componentName)
		{
			return component as UIComponent;
		}
		else if (component.hasOwnProperty("id") && component.id == componentName)
		{
			return component as UIComponent;
		}
		else
		{
			if (component.hasOwnProperty("getChildren") && component.getChildren().length > 0)
			{
				found = findComponent(componentName, component.getChildren());
				if (found && (found.name == componentName || found.id == componentName))
					{
						return found;
					}
			}
		}
	}

	return found;
}

}//class ends


Bookmark and Share

Advertisement

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 )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.