I'm running into something fairly perplexing.  Read the following code and
think of what you'd expect it to do when you click the change button.  Then
run it and click the change button.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
  xmlns:mx="http://www.adobe.com/2006/mxml";
  layout="vertical"
  creationComplete="cc()"
  >
  <mx:Script>
    <![CDATA[
      import mx.collections.ArrayCollection;

      public var d1:ArrayCollection = new ArrayCollection([{label:"one"},
{label:"two"}, {label:"three"}]);
      public var d2:ArrayCollection = new ArrayCollection([{label:"a"},
{label:"b"}, {label:"c"}]);
      //public var d1:Array = [{label:"one"}, {label:"two"},
{label:"three"}];
      //public var d2:Array = [{label:"a"}, {label:"b"}, {label:"c"}];

      private function cc():void
      {
        c1.dataProvider = d1;
        c1.dataProvider = d2;
        c1.selectedIndex = 1;
      }

      private function change():void
      {
        trace();
        trace();
        trace("  before change: ");
        trace("    c1.selectedIndex:", c1.selectedIndex);
        trace("    c1.selectedItem: ", c1.selectedItem.label);

        d1.removeItemAt(0);
        //d1.splice(0, 1);

        trace()
        trace("  after change:  ");
        trace("    c1.selectedIndex:", c1.selectedIndex);
        trace("    c1.selectedItem: ", c1.selectedItem.label);
      }
    ]]>
  </mx:Script>
  <mx:ComboBox id="c1"/>
  <mx:Button label="change" click="change()"/>
</mx:Application>


What does it do?  Well, the combobox changes from saying "b" to "a".  The
trace says the following:
  before change:
    c1.selectedIndex: 1
    c1.selectedItem:  b

  after change:
    c1.selectedIndex: 0
    c1.selectedItem:  b

This is completely baffling.  My only guess is that somewhere in the guts of
combobox or its ancestors, it is registering for changes on a dataProvider
and not unregistering them when the dataProvider changes.  List does not
seem to suffer the same problem.

If you comment out the ArrayCollection-based version and uncomment the
Array-based version, it works as expected.

My next stop is a bug report, but I figured it'd be worth bringing up in
case anyone already knows of a bug report (kind of hard criteria to search
for) or a workaround if you need to use ArrayCollections.

-- 
Jason

Reply via email to