1 person found this helpful
It means that anytime that event(BookEvent.CURRENTPAGE_CHANGED) is dispatched on the object, anyone watching the property returned by the getter (currentPage) will refresh.
[Bindable] public var age:int;
public function get age():int
public function set age( value:int ):void
_age = value;
dispatchEvent( new Event("ageChange") );
are equivalent statements. The advantage is that I can dispatch a "ageChange" event from any function and I don't incur the overheard from dispatching a plain old PropertyChange event. Another instance is when you may have something that you want calculated on the fly or may have complex logic in determing the value to output.
An "ageChange" event on a getter?
If I listen for changes to a value via a setter, my UI can update itself using the new value - good.
Why would I be listening to a getter? Why does it matter to me if someone reads a value?
Because your model or presenter may undergo changes and views and other components may need to know about the new changes.
For instance, on my models for better or for worse, I do something like this
private var _selectedWidget;
public function get selectedWidget():Widget
public function widgetSelected( value:Widget):Widget
//perform model validating and heavy lifting here.
_selectedWidget = value;
//Notify presenters that the selected widget model has changed
// and they need to get crackin.
dispatchEvent( new Event("selectedWidgeChange"));
Forget my comment - it's really all to do with MXML bindings - I'm usually just doing my own thing in actionscript these days.
A better example is in the ListBase class function for "get caretIndex". The "caretChange" event is dispatched in the commitProperties where the caretIndex is changed.
* Item that is currently in focus.
* @default -1
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
public function get caretIndex():Number
It's OK, I understand. I had my wrong thinking hat on.
Oh, you thought I was putting a getter on the UI/MXML component. Are you doing mobile apps, is that the reason for the AS3 only ?
Doing too much stuff with Flash and building AS3 only projects!
Thanks so much, UbuntuPenguin.
Take your example, so suppose that I have a piece of code anywhere:
var age:int = classname.age;
and, if somewhere at runtime age is changed, say, executing statement:
classname.age = 36;
then, will 'var age:int = classname.age;' be re-executed?
Is that right?
Or, should I write the logic explicitly to captue the 'ageChange' event in the place where the statement 'var age:int = classname.age;' resides???
No, var age:int = classname.age; will not be re-executed. But lets think about it for a second, when classname.age is changed, what is the condition of "age", it could be out of scope, or set to a different value and so on. Databinding generally shows itself in mxml components binding to the data they are to represent. If you wanted to bind "age" to classname.age then you must use BindingUtils and set up the binding yourself, it's not difficult but can be weird with its syntax in the beginning.