2 Replies Latest reply on Dec 11, 2008 5:46 PM by shawnyale

    Array Collection and CollectionChange Event

    shawnyale Level 1
      Hi all,
      I have a question that has been bugging me for awhile now and I have not been able to find a good answer. I am using the Flex 3.2 SDK. A typical scenario in my application is to receive a collection of objects that I place into an Array Collection. No in order to have reusable components, inside of my components that use this data, I will create another array collection and set the dataProvider of the component equal to the original arrayCollection.

      Now, in my component creationComplete event handler I usually create an event listener for the arraycollection and assign it a handler. What I've noticed, however, is that my event is never being fired when the outer arraycollection changes. Here is a sample of what I'm doing:


      I can never seem to get the collectionChange event to do anything. I have tried putting the event on the component and the arraycollection with the same results.

      Any help would be appreciated on how to bind the arraycollections so that a change in the collection would trigger an event.
      Cheers.
        • 1. Re: Array Collection and CollectionChange Event
          Level 7

          "shawn.yale" <syale@triad.rr.com> wrote in message
          news:ghs4fl$e02$1@forums.macromedia.com...
          > Hi all,
          > I have a question that has been bugging me for awhile now and I have not
          > been
          > able to find a good answer. I am using the Flex 3.2 SDK. A typical
          > scenario
          > in my application is to receive a collection of objects that I place into
          > an
          > Array Collection. No in order to have reusable components, inside of my
          > components that use this data, I will create another array collection and
          > set
          > the dataProvider of the component equal to the original arrayCollection.
          >
          > Now, in my component creationComplete event handler I usually create an
          > event
          > listener for the arraycollection and assign it a handler. What I've
          > noticed,
          > however, is that my event is never being fired when the outer
          > arraycollection
          > changes. Here is a sample of what I'm doing:
          >
          >
          > I can never seem to get the collectionChange event to do anything. I have
          > tried putting the event on the component and the arraycollection with the
          > same
          > results.
          >
          > Any help would be appreciated on how to bind the arraycollections so that
          > a
          > change in the collection would trigger an event.
          > Cheers.
          >
          > <Application...>
          > <mx:Script>
          > <![CDATA[
          > import mx.collections.ArrayCollection;
          >
          > [Bindable]
          > public var dataProvider:ArrayCollection;
          >
          > private function
          > callSomeWebServiceOrWhateverResultEvent(event:ResultEvent):void
          > {
          > ...
          > dataProvider = event.result;
          > ...
          > }

          In this line you're totally REPLACING the original collection (i.e. this is
          a new different collection) that you put an event listener on.

          So either do this:

          [Bindable]
          public var dataProvider:ArrayCollection = new ArrayCollection();

          private function
          callSomeWebServiceOrWhateverResultEvent(event:ResultEvent):void
          {
          ...
          dataProvider.source = event.result;
          ...
          }

          or do this:

          <myComponent creationComplete="initApp()">
          <mx:Script>
          <![CDATA[
          import mx.collections.ArrayCollection;
          [Bindable]
          private var _componentProvider:ArrayCollection;

          public function get componentProvider():ArrayCollection
          {
          return _componentProvider;
          }
          public function set
          componentProvider(providerCollection:ArrayCollection):void {
          if (providerCollection) {
          _componentProvider.removeEventListener(CollectionEvent.COLLECTION_CHANGE,
          changeHandler);
          _componentProvider=providerCollection;
          _componentProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE,
          changeHandler);
          trace('provider completely replaced');
          }

          private function
          changeHandler(event:CollectionEvent):void
          {
          //will fire on itemUpdated or added.
          trace("This never fires");
          }
          </myComponent>

          I'd suggest the latter, because I wouldn't necessarily bet that the inner
          component will be used in an application that is set up in such a way that
          it won't break the inner component. Your potential for trouble-free reuse
          is to use the second option.


          • 2. Array Collection and CollectionChange Event
            shawnyale Level 1
            I tried both of these solutions and still wasnt able to get the colletionchange event to fire.

            Problems:
            in th first case:
            var dataProvider.source = event.result keeps throwing an error at sompile time that an AttayCollection (the event result) cannot be implicitly converted to type Array (the source on dataProvider). I tried to explicitly convert but receiver the same error.
            but stil doesnt work.

            Using the getter/setter does modify the component property but still doesnt fire te collectionevent. Im wondering if it is a bug???

            Thanks!