• Home
  • Products
  • Technology
  • Experience
  • Services
  • Our Company
  • Blog

Archives

  • February 2009
  • May 2008
  • April 2008
  • March 2008
  • December 2007
  • November 2007
  • October 2007
  • August 2007

Categories

  • Adobe AIR
  • Adobe Flex
  • Cairngorm
  • Encryption
  • General
  • Printing
  • UndoRedo Framework
  • Bookmarks

    • Adobe Labs
    • Planet Ubuntu

Log in
Good, Smart Setter

Yesterday I was fixing some data binding issues and in the process noticed that some of my setters weren’t getting called when they should be — or so it appeared. It turned out that they were just smart.

After a bit of debugging, I had a situation like this:

public function myFunc():void {
    trace(">> calling setter");
    mySetter = "myValue";
    trace("<< done calling setter");
}

And my getter and setter looked something like this:

[Bindable] public function set mySetter(s:String):void {
    trace("called setter");
    // ... do work here
}
public function get mySetter():String {
    trace("called getter");
    return _someVar;
}

Because I had surrounded my setter with trace statements, I could be certain that it was being called. Much to my surprise, my setter was being called… but it wasn’t! In other words, the line containing my call to the setter was being called, but my setter code was never executing. “How could this be?”, I thought.

The output looked like the following:

>> calling setter
called getter
<< done calling setter

I was confused. Admittedly, I’m a Flex newbie, but I’ve never seen anything like this before. So, the first thing I did was checked the livedocs articles on Defining properties as getters and setters, and Defining properties by using setters and getters but they didn’t mention anything about conditionally calling the setters.

Being curious, I decided to take a look to figure out why my getter was being called instead of my setter. The answer was simple. It’s built in! Flex automatically calls the getter before calling the setter. Apparently, if the new value is identical to the old value, it does not call the setter. Thus, the events that I was expecting to be dispatched, were never dispatched.

For the interested, I created a quick demo to test the behavior:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  layout="vertical"
  creationComplete="onComplete()"
  >
 
  <mx:Script>
    <![CDATA[
      import flash.events.MouseEvent;
 
      private var _data:String;
 
      [Bindable]
      public function set mydata(s:String):void
      {
        ta.text += "called set mydata():" + s + "\n";
        _data = s;
      }
 
      public function get mydata():String
      {
        ta.text += "called get mydata():" + _data + "\n";
        return _data;
      }
 
      public function onComplete():void
      {
        mydata = "DATA";
      }
 
      private function onClick(event:MouseEvent):void
      {
        ta.text += ">> changing data to:" + event.target.label + "\n";
        mydata = event.target.label;
        ta.text += "<< done changing data\n\n";
      }
    ]]>
  </mx:Script>
  <mx:HBox width="100%">
    <mx:Label text="mydata:" />
    <mx:TextInput text="{mydata}" />
  </mx:HBox>
  <mx:TextArea id="ta" width="100%" height="100%" />
  <mx:HBox width="100%">
    <mx:Button label="DATA" click="onClick(event)" />
    <mx:Button label="OLD_DATA"  click="onClick(event)" />
  </mx:HBox>
 
</mx:Application>

In the above, the getter/setter pair has value “DATA”. If I click on the DATA button, it only calls the getter because the value is the same. The output in the text area specified above looks like the following:

>> changing data to:DATA
called get mydata():DATA
<< done changing data

And, here’s the stacktrace that shows the the getter being called, even though none of the lines in the setter are ever called.

Traceback showing getter is called

So, the moral of the story is that Flex has good, smart setters … that can really surprise you when you’re not expecting it!

Posted by Kaleb Pederson on October 11, 2007 at 12:12 pm
No Comments or LinksAdobe Flex

Trackback this post | Subscribe to the comments via RSS Feed