Get Distinct ArrayCollection Values

This Utility gives you Distinct values from your source array collection. It iterates through the Source ArrayCollection and gets the item Object for each loop and checks to see if the property value of the item is present in the new Collection. If its not present then it add’s the item to the new Collection, else it ignore’s the item and iterates the next item in the list. This is done by getItemIndex property of the ArrayCollection.

  public static function getDisticntArrayCollection(sourceCollection:ArrayCollection):ArrayCollection{
      var distinctValuesCollection:ArrayCollection = new ArrayCollection();
      for each(var item:* in sourceCollection){
          var propertyValue:Object = item.VALUE;
          if(distinctValuesCollection.getItemIndex(propertyValue) == -1)
             distinctValuesCollection.addItem(item);
       }
    return distinctValuesCollection;
  }
Advertisements

Observe Utility

Observe Utility listen’s property changes at run time on any assigned variable and calls its defined function. This utility does the same job as ChangeWatcher or BindingUtils. I prefer using this as its very light weight, fast and handy.

package org.util
{

    /**
     * Detects changes to a value and runs a function in response.
     * @param source: Source value that is to be watched.
     * @param enabled:Prevents ObserveUtil firing before visual elements are available.
     * @param handler: Function that gets called when source property gets changed.
     *
     * @author Chouhan
     *
     */

    public class ObserveUtil
    {
       public var handler:Function;
       public var enabled:Boolean =true;

       public function set source(source:*):void{
           if(enabled){
                handler.call();
            }
        }
    }
}

USAGE:
<util:Observe
 source="{valueThatIsToBeWatchedFor}"
 handler="{valueChangedFunction}" />

 private function valueChangedFunction():void{
     /* Implement your logic upon property change */
 }

Hope you’ve had fun using this utility. !!