Saturday, September 10, 2011

Flex :: Metadata tag - Event

The [Event] metadata tag is used to define the MXML property for an event and the data type of the event object that a component dispatches.

Following is the syntax for [Event] metadata tag

[Event(name="eventName", type="package.eventType")]

name property of metadata tag specifies the name of the event only.
type property of metadata tag specifies the class that defines the event including entire package.

The [Event] metadata tag is inserted before class defination in an Action Script file or in <mx:Metadata> block ina n MXML file.

If the event is not identify using type and name property defined in [Event] metadata tag then MXML compiler generates an error.

Following is an example:

ActionScript Class

[Event(name="custom_event", type="myPackage.events")]
public class MyClass extends UIComponent
{
   ...
}

MXML Document

<?xml version="1.0"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Metadata>
        [Event(name="custom_event", type="myPackage.events")]
    </mx:Metadata>

    ....
 
</mx:Canvas>

Monday, September 5, 2011

Flex :: Metadata tag - Deprecated

The [Deprecated] metadata tag marks class or class elements deprecated so that the compiler can recognize it and issue a warning when the element is used in an application.

The [Deprecated] metadata tag syntax is as follow

[Deprecated("string_describing_deprecation")]
[Deprecated(message="string_describing_deprecation")]
[Deprecated(replacement="string_specifying_replaceent")]
[Deprecated(replacement="string_specifying_replaceent",                   since="version_of_replacement")]

Insert the [Deprecated] metadata tag before a property, method or class definition to mark that element as deprecated.

[Deprecated(replacement="mycomponent.label")]
public function set text( val:String ):void
{
   ...
}

The [Event], [Effect] and [Style] metadata tag also support deprecation.
These metadata tags support the deprecatedReplacement, deprecatedSince and deprecatedMessage attributes to mark event, effect and style as deprecated.

[Event(... , deprecatedReplacement="string_specifying_replacement",
             deprecatedSince="version_of_replacement" )]

Note: For a command line compiler use show-deprecation-warnings option and set it to true.

Saturday, September 3, 2011

Flex :: Metadata tag - Bindable

The [Bindable] metadata tag enables Flex to copy the value of source property to any destination property, when any changes are made to source property.

The [Bindable] metadata tag has following syntax:

[Bindable]
[Bindable ("eventName")]

The [Bindable ("eventName")] metadata tag register the source property with Flex and dispatches event eventName when property is changed. If eventName is omitted, Flex automatically creates an event named propertyChange so that the property can be used as the source of a data binding expression

Example:


[Bindable]
public var audioLevel:uint = 50;

[Bindable ("nameChanged")]
public function get name():String
{
   return myName;
}

public function set name( val:String ):void
{
   myName = val;
   dispatchEvent(new Event( "nameChanged" ) );
}

There are some situation where data binding does not execute automatically

  • When an item of dataprovider is modified.
  • When a sub-property of a property that have [Bindable] metadata tag is modified.
  • When a [Bindable] metadata tag is associated with a property that Flash player updates automatically, such as mouseY.

There are some method that helps to execute binding that do not occur as expected.

  • The executeBindings() method of UIComponent class helps to execute all the binding for which UIComponent is destinated.
  • The executeChildBindings() method of Container and Repeater classes of Flex executes all the binding for which child UIComponent of Container or Repeater class are destinated.

The executeChildBindings() method updates user interface after making changes that does not cause binding to execute.

However, it should be noted that above method should be called only when binding do not executed automatically.



Friday, September 2, 2011

Flex :: Metadata tag - Array Element Type

In ActionScript language whenever we define an Array variable, we never specify data type of elements that Array will hold.

To allow Flex compiler to perform type checking on Array elements, we can use [ArrayElementType] metadata tag. This metadata tag allow us to specify data type of Array element.

The tag has following syntax:

[ArrayElementType("elementType")]

The property elementType of [ArrayElementType] metadata tag specifies the data type of Array elements, and can be one of the ActionScript data types, such as String, Number, uint, class or interface.

Incase of user defined class, fully qualified class name, including package detail is expected.

For Example:

....

<fx:Script>
<[!CDATA[
   [ArrayElementType("String"]
   public var strProp:Array;

   [ArrayElementType("myPackage.valueObject.data"]
   public var dataProp:Array;
...
]]>
</fx:Script>

....

Note: The MXML complier checks for proper usage of the Array elements only in MXML code; it does not checks Array usage in ActionScript code.