Skip to content

Instantly share code, notes, and snippets.

@lukesh
Created January 20, 2010 21:44
Show Gist options
  • Save lukesh/282304 to your computer and use it in GitHub Desktop.
Save lukesh/282304 to your computer and use it in GitHub Desktop.
// Letting Flex generate PropertyChangeEvents for you
// (most codegen by Flex, most inefficient, easiset to implement.
[Bindable]
public var field1 : String;
// You do everything. You don't necessarily need to fire a PropertyChangeEvent
// to trigger proper binding, as long as the event name == the name specified
// in the metadata.
//
// Note that [fieldname]Change is the Flex default, which means if you specify:
/*
[Bindable]
public interface IMyStruct {
public function set field(value : Type) : void;
public function get field() : Type;
*/
// The name of the PropertyChangeEvent that Flex generates will happen to be
// "fieldChange" -- so you can create lightweight interfaces (meaning you don't
// need to explicitly set [Bindable] on all the get/set pairs) and implementations
// will still be proper when you manually specify them.
private var _field2 : String;
[Bindable(event="field2Change")]
public function get field2() : String {
return _field2;
}
public function set field2(value : String) : void {
if (_field2 != value) {
_field2 = value;
dispatchEvent(new Event("field2Change"));
}
}
// Private setter public getter (read-only property).
// Remember bindings work by sending events when the value of the field changes,
// so even if it's private it must be sent.
private var _field3 : String;
[Bindable(event="field3Change")]
public function get field3() : String {
return _field3;
}
private function setField3(value : String) : void {
if (_field3 != value) {
_field3 = value;
dispatchEvent(new Event("field3Change"));
}
}
// In order to set a read-only property that bases its value on other properties,
// you *must* send the corresponding binding event when any related property fires.
private var _field4 : Number;
[Bindable(event="field4Change")]
public function get field4() : Number {
return _field4;
}
public function set field4(value : Number) : void {
_field4 = value;
dispatchEvent(new Event("field4Change"));
dispatchEvent(new Event("field5Change"));
}
[Bindable(event="field5Change")]
public function get field5() : Number {
// You can return a calculated result here
return _field4 * 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment