explorers’ club


A common {Binding} issue and a work around.
Thursday, March 29, 2007, 1:53 pm
Filed under: actionScript, development, flex

If you are relying on the built in Flex binding mechanism, you may have run into this issue: The Binding won’t update its listeners if the value you are setting the property to is the same as the previous value. Put another way:

var a:Number = 10;
trace(a); //firing the getter which outputs 10.
a = 10; //firing the setter, bindings will not update listeners.

Do not rely on Flex to fire the propertyChangeEvent w/ respect to the related getter/setter by using the default [Bindable] metatag. Instead modify your class with these steps:

  1. Instead of [Bindable], use [Bindable (event="myCustomEventName")] on your getter.
  2. Make sure the related setter dispatches the event like so: dispatchEvent(new Event("myCustomEventName"));
  3. Also making sure to add [Event (name="myCustomEventName", type="flash.events.Event")] for any external listeners.
  4. Implement custom logic on the listeners in questions so that they can be updated via the event rather than the binding.*

How this might assist you? Well rather than relying on having to circumvent the binding not firing when setting the property to the same value, you could have additional listeners for that custom event since it is fired regardless of the previous set value.

*Although I do use this method on all of my getters/setters, I would not necessarily implement the external logic on all of the bound listeners. I would only do so in cases where the property needs to be updated regardless of whether or not the new value is the same as its previously set value.


7 Comments so far
Leave a comment

I’m pretty sure the default change event is propertyName + “Change”, so if your property was “firstName”, then you’d just dispatch “firstNameChange”… or was it past-tense, “firstNameChanged”. Anyway, no need make your own event if you can figure out if it has the d or not, hehe!

Comment by JesterXL

Correct. I wasn’t implying that you need to make an Event subclass, I am simply suggesting, as you have reiterated, to create your own event type as shown in the code example up above. So new Event(‘newEventType’);

Comment by jwopitz

[...] you’d make use of enumeration property in the Style metatag. Also, anyone who has read my post on Flex’s built in Binding mechanism might want to implement their own custom event/binding [...]

Pingback by Tips (and reckless behaviour) for Component Developers « jwopitz - flex/flash exploration

I faced a similar problem with binding when implementing a read-only data model. Check it out here: http://adamflater.blogspot.com/2007/08/binding-with-getters-and-setting.html

-adam

———-
http://adamflater.blogspot.com

Comment by Adam Flater

Oh, and did not know about it. Thanks for the information …

Comment by Andy

Does anyone have a more complete example of solving this binding proble? I’m facing the same, and having problem to find a solution.
Thanks for any info!

Comment by Romain

it dosnt’ works if you have getter function in a class and use this function as a dataprovider then making any change it dataprovider is updated same in getter property too…

Comment by sanjeev




Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>