Home > Component, Flex, code > Dedupe ArrayCollection Component

Dedupe ArrayCollection Component

November 26th, 2008

Building off of my last post, I thought I might as well create a deduped arraycollection as well, just in case anyone wanted to use the data for something other than a combobox.  The implementation for this component is very similar to that of the combobox.  The DedupeArrayCollection allows you to pass it an array (one that has some kind of properties to each of the items within it), then set the dedupeProperty to whatever field you wish to dedupe.

The DedupeArrayCollection:

package com.archfamily {
 
	import flash.events.Event;
	import flash.utils.Dictionary;
 
	import mx.collections.ArrayCollection;
	import mx.events.CollectionEvent;
 
	public class DedupeArrayCollection extends ArrayCollection {
 
		private var _dedupeProperty:String = "";
 
		public function set dedupeProperty( value:String ):void {
			_dedupeProperty = value;
			this.source = this.source;
		}
		public function get dedupeProperty():String {
			return _dedupeProperty;
		}
 
		/**
		 * 
		 * Needed to override the standard dataprovider in order to reset
		 * the duplicate value each time
		 * 
		 */
		override public function set source( value:Array ):void {
			var _returnArray:Array = value;
 
			if ( value && dedupeProperty.length > 0 ) {
				var _map:Dictionary = new Dictionary( true );
 
				value.forEach( function( item:*, index:int, array:Array ):void {
					_map[ item[ this ] ] = item; // in the loop, this == dedupeProperty
				}, dedupeProperty );
 
				_returnArray = [];
				for each ( var object:Object in _map ) {
					_returnArray.push( object );
				}
			}
 
			super.source = _returnArray;
		}
 
		public function DedupeArrayCollection( source:Array=null ) {
			super( source );
		}
 
	}
}

The implementation of the arraycollection:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" xmlns:archfamily="com.archfamily.*">
	<mx:Script>
		<![CDATA[
			import com.archfamily.DedupeArrayCollection;
 
			[Bindable] private var ac:DedupeArrayCollection = new DedupeArrayCollection( [ { name: "car", total: 100 },{ name: "bus", total: 200 },{ name: "bike", total: 300 },{ name: "train", total: 200 },{ name: "boat", total: 400 },{ name: "car", total: 500 },{ name: "bus", total: 200 },{ name: "convertible", total: 100 },{ name: "gizmo", total: 300 },{ name: "boat", total: 300 },{ name: "car", total: 100 },{ name: "dune buggy", total:300 },{ name: "motorcycle", total: 100 }] );
 
			public function init():void {
				ac.dedupeProperty = "total";
			}			
		]]>
	</mx:Script>
	<mx:VBox>
		<mx:ComboBox id="testing" dataProvider="{ ac }" labelField="name" />
	</mx:VBox>
</mx:Application>

The only thing I would probably change is to figure out what event needs to fire when my dedupeProperty is set to get the arraycollection to reload the source. Currently I’m just setting the source to itself so that the event fires and reloads the arraycollection, but just firing the event would probably be less overhead.

Gareth Component, Flex, code , , , ,

  1. Derrick Anderson
    June 11th, 2009 at 09:58 | #1

    very handy function, well done!

  2. Austin
    June 14th, 2009 at 13:32 | #2

    Thanks a lot for this post. Works great.

  1. No trackbacks yet.