3

I'm implementing some kind of combobox control (by extending spark.components.supportClasses.DropDownListBase)

Now, inside this control; I need to know:

  1. if the dataprovider is changed/assigned. (which I can do... the first approach below works);
  2. if any item in the dataprovider collection has changed.

I tried 2 methods that did not do the trick...

1ST APPROACH:

        [Bindable("collectionChange")]
        override public function set dataProvider(value:IList):void
        {
            if (value) value.addEventListener(CollectionEvent.COLLECTION_CHANGE, onDataChange);

            super.dataProvider = value;
            trace("DATA CHANGED"); //fires
        }

        protected function onDataChange(event:CollectionEvent):void
        {
            trace("COLLECTION ITEM(S) CHANGED"); //does not fire
        }

2ND APPROACH:

Since this is based on DropDownListBase; it should dispatch the CollectionEvent.COLLECTION_CHANGE event already..?

        public function myClass() //constructor
        {
            addEventListener(CollectionEvent.COLLECTION_CHANGE, onDataChange);
        }

        protected function onDataChange(event:CollectionEvent):void
        {
            trace("DATA CHANGED"); //does not fire
        }

Any ideas?

UPDATE: Edited above.. The first approach lets me know if the dataprovider is changed but not if any item is updated in the dataprovider collection. The second approach does not work at all..

zero323
  • 322,348
  • 103
  • 959
  • 935
Onur Yıldırım
  • 32,327
  • 12
  • 84
  • 98

1 Answers1

1

We'll be able to help significantly more if you show us a runnable sample to demonstrate the problem.

  1. if the dataprovider is changed/assigned.

Your first approach should work. Can you tell us what makes you think it didn't? ( No trace statement I assume? ). And tell us what you did to change the dataProvider.

The second approach won't work because myClass is not firing off the collectionChange event.

2 if any item in the dataprovider collection has changed.

There is not really a way to tell this. In most cases, a collection is just a list of pointers to other objects. If you change those pointers, then a collectionChange event is fired. IF you change the item that he pointer is pointed to, the collection has no way to know that something changed. Binding works very similarly if your an MXML fan.

If you have control over how items are changed, you can deal with it that way. Instead of:

(collection.getITemAt(x) as myObject).property = newValue;

Do something like this:

  var myObject : MyObject = collection.getITemAt(x) as myObject
  myObject.property = newValue;
  collection.setItemAt(x, myObject);

I would expect that to fire a collectionChange event, but not the former.

That said, in the context of a dropDownListBase: As you scroll or open and close the drop down, the itemRenderers should be updated to reflect the most current dataProvider's data. But if you change something on the fly while the drop down is open, I would not expect it to update automatically [if you're not changing the dataProvider.

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • myClassInstance.dataprovider = someArrCollection; //this works.. but myClassInstance.dataprovider[0].name = "john"; //will not fire the collectionChange event. Is there a way to know when a property of a collection item has changed? so that I can update my combo list labels.. – Onur Yıldırım Jan 10 '11 at 22:22
  • Since dataProvider property is [Bindable("collectionChange")]; I think the owner class should fire a collectionChange event. – Onur Yıldırım Jan 10 '11 at 22:25
  • @Radgar To answer your first question, Not that I know of. When something changes, you can usually invalidate something to trigger the update; but nothing in the component or collection knows. [Bindable('collectionChange')] does not necessarily mean the owner class fires a collectionChange event, but you can definitely look for that event in the component or it's parent's to see if it does fire and when. – JeffryHouser Jan 10 '11 at 23:29