nwebb

Flex, Flash, AIR

One Reason Why Your Flex Getter May Not Execute

I ran in to a nasty little bit of behaviour [bug?] with a pair of bindable getter/setter methods today, and it's something you should proabably make yourself aware of because it's pretty easy to run in to and not immediately obvious what the cause is.

Problem
The code in my setter method was only being run the very first time the method was called. After that it didn't run at all - I was stumped.

I placed a breakpoint at the first line of the setter, and then on the second line (which was just a curly brace), and the third line ... you get the picture. The first breakpoint was always triggered, but the second and third breakpoints only got 'hit' the first time I called the setter - after that they were completely ignored. So, the code didn't appear to be the problem. I placed a trace at the beginning of the setter and even that got ignored after the first run.

Issue
Normally, when I create a setter method it looks something like:

Actionscript:
  1. [Bindable]
  2. public function set someValue(value:Object):void
  3. {
  4.         if(value != _someValue)
  5.         {
  6.                 //code here...
  7.         }
  8. }

... but on this occassion I had some logic which I wanted to execute even if the value being sent in was the same as last time, and this works fine ... until you make the getter/setter bindable

Cause
As soon as you make the getter/setter bindable, Flex stops running the setter whenever the value you're sending in already matches the value that the corresponding getter will return.

Solution
The solution (or at least one solution) is not to rely upon the default propertyChange event which is dispatched for bindable values, and instead create/dispatch a custom event like so:

Actionscript:
  1. [Bindable(event="someValueChange")]
  2. public function set someValue(value:Object):void
  3. {
  4.        //code here... this will run regardless
  5.         if(_someValue != value) _someValue = value;
  6.         dispatchEvent(new Event("someValueChange"));
  7. }
  8.  
  9. public function get someValue():Object
  10. {
  11.         return _someValue;
  12. }

This ensures that the setter is run regardless. My problem was that the second time I called the setter I was (you guessed it) passing in the same value as the getter was already returning.

I'm sure the reason the setter's ignored is performance-related, and using a custom event is no big deal - the issue was actually working out what was causing this behaviour in the first place.

The issue was also reported by Tony Hillerson ( thanks to Tony and the guys on his blog who left comments, for identifying the cause and saving me some time =) )

1 comment

FOTB Tickets Going Cheap (possibly!)

The organisers of Flash On The Beach have just put 5 full 3-day passes up for grabs on Ebay. At the time of writing the highest bid is only £51

ticket 1, ticket 2, ticket 3, ticket 4, ticket 5

Get bidding :)

No comments

Firefox Tip - Opening Flex Apps In The Current Tab

Like many developers I use Firefox as my default browser. One extension I wouldn't want to be without is Tab Mix Plus. Since installing Firefox 3 I've been using the dev build, but this morning I foolishly accepted the 3.0.1 browser update only to find that the extension is not compatible with that particular release.

When I'm testing my Flex apps I like the .swf to open up in the same window each time (Tab Mix Plus has a handly little option to allow this). It means I don't end up with multiple tabs open when testing my app.

If that option wasn't available, for testing purposes I guess I would want to automatically switch to the newly opened tab automatically, but that still means I'd end up with multiple tabs open and it is something that would drive me crazy when simply surfing the web normally (and toggling between those two options depending on what I'm doing is a non-starter!), so with TMP not currently working I did a quick search to find out how to set the "open in current tab" preference myself. Here it is in case you would like to do it:

1) Open Firefox and type about:config in to the address bar.
2) You'll see the "Here Be Dragons!" warning. Click the "I'll Be Careful I Promise" button.
3) Use the filter bar and type in browser.link
4) Set browser.link.open_external to 1 (this is why)

That's it. Now whenever you test your Flex app it should open up in the current tab. If this annoys you then you can always change it back to the default (3). I guess whether you like it or not depeneds on your surfing habits. During normal surfing I tend to right-click links and select "open in new tab", so I find setting this to 1 gives me the ideal setup.

2 comments

Next Page »

Bad Behavior has blocked 1276 access attempts in the last 7 days.