4 Replies Latest reply on Feb 26, 2008 6:07 AM by m_hartnett

    n00b question about CollectionEvents

    robmcmi Level 1
      Hi, thanks in advance!

      I have adopted the cairngorm framework for a rebuild of my first large flex project, and developing using MXML more than before is proving a little harder.

      I have a view (canvas) with a bindable array collection, which is bound to an array collection in the model locator.

      However I want to have an collection change event in my view to generate new pages in a cover flow container.

      The problem is adding an event listener to the array collection. If I do it via creationComplete="onCreationComplete();" in the view it doesn't work. If I add the listener in a user triggered event (which is at a later point in time to the creation complete) it works. However I need it to get added automatically asap to the application loading.

      The code I am using is: myArrayCollection.addEventListener( CollectionEvent.COLLECTION_CHANGE , collectionEventHandler );

      Thanks again

      Rob
        • 1. Re: n00b question about CollectionEvents
          robmcmi Level 1
          I managed to test the theory by binding the array collection to a datagrid and then adding the event to the data grid's updateComplete property in the MXML.

          So how do I add an event to an array collection in a more direct manner!

          I know this must be really easy, I expect I need to wait for the array collection to be initialised or have it's first data inserted or something. (I have tried = new ArrayCollection(); before adding the listener with no results)
          • 2. Re: n00b question about CollectionEvents
            robmcmi Level 1
            If I have a bindable ArrayCollection and add a listner to it, then elsewhere in the code I change the ArrayCollection it's bound to by saying: someArrayColleciton = tempArrayCollection will it loose events?

            If so should manually add elements?

            Rob

            • 3. Re: n00b question about CollectionEvents
              robmcmi Level 1
              I figured it out!!!!

              After using various different methods most of them had the same problem. However if I used a timer to trace the length I could see it changing, but the event listener wasn't being triggered!

              The problem was elsewhere I was setting the bound array collection like so:

              bound_array_collection = new_temp_array_collection

              This was then causing the event listener assigned to the bound_array_collection to be overwritten too. Even setting the source of the collection like this caused the listener to be lost.

              So instead I have to now use the addItem on the array collection.

              This seems to work find.

              Hope this helps someone else!

              Rob

              • 4. Re: n00b question about CollectionEvents
                m_hartnett Level 3
                We struggled with the same issue.
                In our case we have an ArrayCollection that holds ArrayCollections.
                This data is populated on application startup and it is indexed in the Outer ArrrayCollection by some name. If AC is an ArrayCollection in the model locator then we would add data to it as follows. Of course this is done in separate Cairngorm calls.

                AC["companyInfo"] = new ArrayCollection() (xlm returned as AC)
                AC["navigationInfo"] = new ArrayCollection() (xlm returned as AC)

                At the same time the model locator contains another ArrayCollection used to dispatch events. This is the same design as the data AC in that it is indexed by some name.
                ACLsnr["companyInfo"] = new ArrayCollection();
                ACLsnr["navigationInfo"] = new ArrayCollection();

                The listener ArrayCollection will always be empty but will never have its listeners destroyed each time the ArrayCollection is reloaded.

                When the data is reloaded, the Cairngorm command object refreshes the ArrayCollection that holds the data and at the same time issues something like this:

                var o1: ArrayCollection = model.ACLsnr["companyInfo"] as ArrayCollection;
                if(o1 != null)
                o1.dispatchEvent(new DataLoadEvent(parmObj.maintType + "LoadEvent", "success"));

                This way, the listeners are always kept on the listener arraycollection. They can be added and removed when needed.

                Hope this is clear or at least gives you an idea of how we work it.

                Though it is a bit of work getting up on Cairngorm, you will not be sorry for moving to it. It lends a great deal of organization and implements some great design patterns for your application.