CollectionClone

Recently I was using Adobe’s AutoComplete component and a problem cropped up, it applies a filter to the original dataProvider assigned to it.

This was a problem because I was using the AutoComplete to give the user some sort of code completion on a list of classes, and I had multiple instances of this AutoComplete component in my application. When the user used an AutoComplete component, the original collection used as the dataProvider was filtered which meant that any previously selected items in other AutoComplete instances had a good chance of being removed from the list.

You can see this in the example by selected ‘United Kingdom’ in the first AutoComplete component, then typing the letter ‘A’ in the second, and you’ll see that the first component no longer retains its selectedItem, as it is no longer in its dataProvider.

CollectionClone can be used to duplicate a collection before assigning it as a dataProvider, leaving the original dataProvider unfiltered, and only filtering the clone. It will keep an eye on the original collection for any changes, and make sure that they are all reflected in the cloned collection.

CollectionClone Example

The source for CollectionClone and be downloaded from our Google Code repository

14 Responses to “CollectionClone”

  1. BoiNoi says:

    Can’t you just do this with the listcollectionview? Like what is mentioned here: http://www.briankotek.com/blog/index.cfm/2009/3/18/Flex-Tip-Why-ListCollectionView-Is-Your-Friend

    Am I missing the point?

  2. This is actually what ListCollectionView (http://livedocs.adobe.com/flex/3/langref/mx/collections/ListCollectionView.html) is used for. You can create a new LCV for each place that needs an independent filter applied, but they all use the same ArrayCollection as their source. I have posted a short example here: http://www.pastie.org/595658

    HTH,
    Ben

  3. Tink says:

    @BoiNoi

    No you aint missing the point at all mate, just wish I knew LCV did that!

    @Ben

    Doh, thanks!

    Useful stuff (especially if i’d have knew yesterday) ;)

  4. Hehe, I had the same reaction back when I discovered LCV.

  5. todd says:

    @Ben, Thanks. I’ve learned something new today. And Thanks Tink, we were having similar problems and we decided to just start cloning our Arrays…ouch.

  6. Rob says:

    Hey Tink.

    This looks really useful and could potentially solve a problem I have with sorting and searching in collections using cursors (which I think is the basis of your problem here).

    I posted a question about this on stack overflow, but as of yet haven’t had anyone provide me with a decent solution, that doesn’t involve costly lookups or refreshing the array.

    http://stackoverflow.com/questions/1406620/searching-a-collection-in-flex

    I think there is a gap in the framework for having multiple cursors on a collection, or having a collection with multiple views. What do you think?

  7. Tink says:

    @Rob

    This was do do with sorting, but not wanting to sort the original collection. As mentioned in the comments above, Adobe already have a class for this in place ‘ListCollectionView’ so my class is pretty much useless.

    On your problem, why do you have to pass through the published property in the findFirst() method. Can’t you just ignore the published property and search by ID alone?

  8. Rob says:

    @tink,

    If you are using the findFirst() function you have to pass though the same parameters that you used in the sort. You can leave off sort filters that are lower down in the chain but not higher up.

    Therefore if I sort by, “id, publishedDate” then I could search by id only, but if I want to sort by “publishedDate, id” adding the id purely so I can search and for no other reason, I have to pass though “Date, ‘id” to the search.

    It’s just really annoying that searching is coupled to sorting, it doesn’t seem to make sense.

    Perhaps I could create a new ListCollectionView, I’ll have a look into that now!

  9. Tink says:

    You could just loop through the collection yourself to find the item?

  10. Rob says:

    @Tink

    I could, but I want to use findFirst() on the cursor, in order to move it to the article the user has selected. I have a number of other controls bound to the cursors current item.

    It all works very well, until you want to sort and search using different criteria :(

    I guess I will just have to pass the object they want around, so I can almost search using any of it’s parms.

  11. Tink says:

    right but you could use loop, search for it yourself. Once you hit it exit the look, the more the cursor to that index using

    _cursor.seek( CursorBookmark.FIRST, index );

    I presume the findFirst() method just loops through the items anyway?

  12. Rob says:

    More or less. I think it jumps around so things at the end don’t take the longest to find.

    It more that I want to use the framework how your meant to, get the best out of it etc

    For now I can do what you said or, just pass through the date too. Shame you just can’t pass through wildcards in the search…

    Thanks for the help

  13. sysaux says:

    Hi, Tink!

    I’m Flex developer, and i interested on extension of “efflex” library.
    Yesterday i found bug in spark-driven efflex lib ))). I’m want to code spark-based effects in your library. How you see to it?

    *sorry for my bad English.

  14. Tink says:

    Hi sysaux

    Sorry for the slow reply your comment was lost in the spam.

    I’d hold off for a while on extending the spark-driven efflex library as it’s going to change a lot when I get some time to dedicate to it.

Leave a Reply