The benefit is that your getter function
will be bound to that event, so any time that event is dispatched, the data binding
is updated. In this example, it doesn’t really stand out that this
is a great thing. Let’s say that there is also a “get
lastname” function that is similar to your firstname function, and add an
updateNames function, so we have this…
[Bindable(event="MyEvent")]
public function
set
firstname(value:
{
_firstname = value;
dispatchEvent( new FlexEvent('MyEvent') );
}
public function
get firstname():
{
return _firstname;
}
private var
_firstname:String;
[Bindable(event="MyEvent")]
public function
set lastname(value:
{
_lastname = value;
dispatchEvent( new FlexEvent('MyEvent') );
}
public function
get lastname ():
{
return _lastname;
}
private var
_lastname:String;
public function
updateNames (
myUpdateObject : Object ):
{
_firstname = myUpdateObject.firstname;
_lastname = myUpdateObject.lastname
dispatchEvent( new FlexEvent('MyEvent') );
}
The updateNames function dispatches a
single event, which updates the bindings on both of your “getter”
functions. Now, the real benefit of “set” functions is
that you can programmatically execute code when a value is updated. Your
names example doesn’t take full advantage of the capabilities. The “set”
function is better suited for when you have other functions that should be
executed any time that your “set” is updated.
[Bindable(event="MyEvent")]
public function
set
firstname(value:
{
_firstname = value;
//execute
some code here
doSomething();
doSomethingElse();
doAnotherThing();
dispatchEvent( new FlexEvent('MyEvent') );
}
In this example, the data binding gets
updated after the other functions have been executed. This technique is
VERY useful when developing flex components. For instance, you can have a
function on a custom component that extends a text box such as:
override public function set text( value :
String ) : void
And you want other properties to be
updated when text has been updated, so you setup your “set”
function like this:
override public function set text( value :
String ) : void
{
this.updated
= true;
resetComponentState();
super.text
= value;
}
Hope that helps,
-Andy
_____________________________________
Cynergy Systems, Inc.
Blog: http://www.cynergysystems.com/blogs/page/andrewtrice
Email: andrew.trice@...
Office: 866-CYNERGY
From:
Sent: Tuesday, November 28, 2006
9:02 PM
To:
Subject: [flexcoders] Databinding
Custom Event
Hey,
What if any benefits are there to defining a custom event type for your
databinding?
ie:
[Bindable(event="MyEvent")]
public function
set
firstname(value:
{
_firstname = value;
dispatchEvent( new FlexEvent('MyEvent') );
}
public function
get firstname():
{
return _firstname;
}
private var
_firstname:String;
vs
[Bindable]
public var
lastname:String;
Regards,
Bjorn