Abdulcode

Pages

Jul 12, 2010

Flex: ChangeWatcher Class

Today I studied across a handy utility class called ChangeWatcher, which lives inside the mx.binding.utils package. It provides you with a handy way of watching other object’s properties.

Syntax :
ChangeWatcher.watch(object, "property", handler);

Object – the object which owns the property
"property" – the property you want to watch
handler – the function name of the handler
Whenever the property of the object changes, the handler will respond.
The property that ChangeWatcher is watching must be [Bindable] and Public.

Sample :
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
creationComplete="initApp();">
<mx:Script>
<![CDATA[
import mx.binding.utils.ChangeWatcher;
import mx.controls.Alert;
import mx.events.PropertyChangeEvent;

[Bindable]
public var tmpData:String="welcome";

private function initApp():void
{
ChangeWatcher.watch(this, "tmpData", watchHandler);
}

private function watchHandler(e:PropertyChangeEvent):void
{
// Do Something
}

private function clickHandler():void
{
if (tmpData == "welcome")
tmpData=tmpData.toUpperCase();
else
tmpData=tmpData.toLowerCase();
}
]]>
</mx:Script>

<mx:Label text="{tmpData}"/>
<mx:Button click="clickHandler()"/>

</mx:Application>

No comments:

Post a Comment