Say you have a bindable component that has many properties, some are read-write and others are read-only. Then you decide that you want to bind one of the read-only properties to something else. What you will notice is the compiler spewing out the following warning:
[Bindable] on read-only getter is unnecessary and will be ignored.
Looking a bit at the documentation on the matter, I found this gem:
You must define both a setter and a getter method to use the [Bindable] tag with the property. If you define just a setter method, you create a write-only property that you cannot use as the source of a data-binding expression. If you define just a getter method, you create a read-only property that you can use as the source of a data-binding expression without inserting the [Bindable] metadata tag. This is similar to the way that you can use a variable, defined by using the const keyword, as the source for a data binding expression.
Essentially, what Adobe is saying is that properties should never change from within, which makes no sense since most components have their own interactions which are handled internally. This is another example of Adobe’s incompetence in software engineering; to think that a property can’t change internally is just short-sighted.
Fortunately for us, there is of course a workaround to save the day. The solution is to create a getter-setter property for the bindable get property that dispatches a ‘propertyChanged’ event which then updates all the listeners. Did I just blow your mind-hole? Here’s a quick example:
// Internal Variable
private var _state:String = "someState";
// Bindable read-only property
[Bindable(event="stateChanged")]
public function get state():String
{
return stateProperty;
}
// Internal Getter-Setter
private function get stateProperty():String
{
return this._state;
}
private function set stateProperty(value:String):void
{
this._state = value;
dispatchEvent(new Event("stateChanged"));
}
Now in your application, just need to bind to the ‘state’ property like you normally would and that’s all there is to it. I find it quite annoying that you have to to add several extra line of code to something that should be done automatically, but it works for now. The JIRA ticket for this specific bug actually mentions that it will be fixed in a future release, and I will be looking forward to it.
Copyright © Thinking in Code. All Rights Reserved. Powered by Wordpress