Example 3 - Calling static functions

You can see that com.fvf.* does not have all classes implemented, but this does not mean that we cannot use non-implemented Flex classes. We can access any Flex class using com.fvf.FlexInterface. There are two static methodes:

FlexInterface.getClass(className:String) : Class;
FlexInterface.getInstance(classname:String) : DisplayObject;

FlexInterface.getInstance() will create instance of any Flash or Flex class. E.g.

var myButton = FlexInterface.getInstance("mx.controls.Button");
myButton.label = "Click me";
myButton.toolTip = "Flex tooltip";
application.addChild(myButton);


Note: you can see that we do not need to call toDisplayObject() to add coponent, becuase myButton is now pure Flex component.

Similar to FlexInterface.getInstance(), FlexInterface.getClass() will return class definition. After we get class definition we can call any static function:

var alertClass : Class = FlexInterface.getClass("mx.controls.Alert");
alertClass.show("Calling static function");

which is same as in Flex:

mx.controls.Alert.show("Calling static function");


package  
{
	import com.fvf.FVFApp;
	import com.fvf.FlexInterface;
	import com.fvf.controls.FVFButton;
	import flash.events.MouseEvent;
	
	public class ExampleApplication extends FVFApp
	{
		public function MyApplication() {
		}
		
		override protected function run() {
			var myButton:FVFButton = new FVFButton();
			myButton.label = "Flex button";
			myButton.toolTip = "Flex tooltip";
			myButton.addEventListener(MouseEvent.CLICK, onButtonClick);
			
			application.addChild(myButton.toDisplayObject());
		}
		
		private function onButtonClick(e:MouseEvent):void {
			FlexInterface.getClass("mx.controls.Alert").show("Hello Flex from flash.");
		}
	}
}

exmpl3.jpg

Last edited Aug 29, 2008 at 12:54 PM by PasalicZaharije, version 9

Comments

No comments yet.