Ok - I think I'm making some headway now.  Here's hoping this helps
someone else with similar problems.

For ages I've been plagued by 'odd' databinding problems.  Things
would work most of the time - then fail for 'no reason'...

Playing around tonight, I noticed some behaviors that I think explain
a bunch of the problems I've been having.  Perhaps others have had the
same problems, so here it is in a nutshell:  It appears eventlisteners
and databinding bind to the object - and _not_ the identifier/label.
This holds true even for accessor based properties.

This means that having a 'currentRec', that gets reset to point to
each object in a list for instance, can be very dangerous.  for
instance, if you have
  var currRec:MyRecord
and you use it in mxml databindings, the bindings get applied to the
object currRec reference when the component was initialized!  If you
later move another record into currRec, your databindings still point
to the FIRST object.

Could someone from Adobe confirm this?

Below is a little toy I wrote to test this stuff.  The trace output
supports the statement above.  Notice how after the assignment: baz =
bar, we start getting
trace output from the 'bar' eventlisteners. The 'baz' eventlisteners
no longer fire.
Play around with some of the commented out statements, and see how the
output changes - I was really surprised that statement ordering in the
setter made a difference!

Btw, If anyone can tell me why the PropertyChange handler doesn't
fire, I'd appreciate it!  I thought 'PropertyChange' was the default
event used by databinding?

Trace output:
Assign 1 model to another
BAZ:Model Changed Event Generated!  Event type: ModelChanged

Assign to accessorType
BAR:Model Changed Event Generated!  Event type: ModelChanged

Assign to var based property - Why doesn't the 'PropertyChange' handler fire?
call ModelBase.set()
BAR:Model Changed Event Generated!  Event type: ModelChanged

Test completed baz.accessorType: Steve
BAR:Model Changed Event Generated!  Event type: ModelChanged



==================================================
ModelBase1.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute" creationComplete="setup()">
  <mx:Script>
    <![CDATA[
     import ModelBase;
     import DerivedClass;
     import mx.binding.utils.BindingUtils;
     import flash.events.*;

    private var _baz:DerivedClass = new DerivedClass();

    [Bindable (event="ModelChanged")]
    public function get baz():DerivedClass { return _baz; }
    public function set baz(v:DerivedClass):void {
      // Notice you get different handlers depending on which dispatch you use
      // also, the 'this.baz' is needed because 'this.dispatch' or
'dispatch' would
      // dispatch on the root tag (this = mx:Application)
      this.baz.dispatchEvent( new Event("ModelChanged"));
      _baz = v;
      //this.baz.dispatchEvent( new Event("ModelChanged"));
    }

    [Bindable (event="ModelChanged")]
    private var foo:DerivedClass = new DerivedClass();
    [Bindable (event="ModelChanged")]
    private var bar:DerivedClass = new DerivedClass();

     public function setup():void {

       foo.addEventListener('ModelChanged',   foomodelChanged);
       foo.addEventListener('PropertyChange', foopropertyChanged);
       bar.addEventListener('ModelChanged',   barmodelChanged);
       bar.addEventListener('PropertyChange', barpropertyChanged);
       baz.addEventListener('ModelChanged',   modelChanged);
       baz.addEventListener('PropertyChange', propertyChanged);

       trace("Assign 1 model to another");
       baz = bar;

       trace("\nAssign to accessorType");
       baz.accessorType = "Steve";

       trace("\nAssign to var based property - Why doesn't the
'PropertyChange' handler fire?");
       baz.varType = 'Whee!';

       trace("call ModelBase.set()");
       baz.set(foo);

       trace("\nTest completed baz.accessorType: " + baz.accessorType);
     }

    public function changeit():void {
      //bar.accessorType = 'Changed';
      baz.accessorType = 'Changed';
    }

    public function propertyChanged(e:Event):void {
      trace("BAZ:Binding detected a change!");
    }
    public function modelChanged(e:Event):void {
      trace("BAZ:Model Changed Event Generated!  Event type: " + e.type);
    }

    public function barpropertyChanged(e:Event):void {
      trace("BAR:Model Changed Event Generated!  Event type: " + e.type);
    }
    public function barmodelChanged(e:Event):void {
      trace("BAR:Model Changed Event Generated!  Event type: " + e.type );
    }

    public function foopropertyChanged(e:Event):void {
      trace("FOO:Model Changed Event Generated!  Event type: " + e.type );
    }
    public function foomodelChanged(e:Event):void {
      trace("FOO:Model Changed Event Generated!  Event type: " + e.type );
    }

    ]]>
  </mx:Script>
  <!-- I'd have expected this to work, but it doesn't as the object referenced
       by baz changes  -->
  <!--mx:Button id='b1' label="{baz.accessorType}"  x="53" y="109"/>
  <mx:Button id='b2' label="{baz.varType}" click="changeit()" x="53"
y="130" / -->

  <!-- This works since bar always refers to the same object -->
  <mx:Button id='b1' label="{bar.accessorType}"  x="53" y="109"/>
  <mx:Button id='b2' label="{bar.varType}" click="changeit()" x="53" y="130" />
</mx:Application>



==================================================
DerivedClass.as:
package {
  import flash.events.Event;
  public class DerivedClass extends ModelBase {

    private var _aType:String = "Start";

    // How do we bind to a custom event in mxml ?
    //[Bindable (event="ModelChanged")] - doesn't work for ModelBase1.mxml
    [Bindable]
    public var varType:String = "variable Based";

    [Bindable (event="ModelChanged")]
    public function set accessorType(v:String):void {
      _aType = v;
      dispatchEvent( new Event("ModelChanged"));
    }
    public function get accessorType():String { return _aType; }

    public function DerivedClass() {
      //
    }

  }
}

==================================================
ModelBase.as
package  {      
  import flash.utils.*;
  import flash.events.*;
        
  public class ModelBase extends EventDispatcher {      
        public var dummy1:String = 'String';
                
        public function ModelBase() { }         
        
    public function set(o:ModelBase):void {
      this.dummy1 = o.dummy1 + " blah";
      dispatchEvent( new Event('ModelChanged'));
    }   
  }
}

Reply via email to