0 Replies Latest reply on Apr 2, 2008 6:31 PM by craigmcdonnell

    ArrayCollection add item event handler useful for validation

    craigmcdonnell
      Can we use ArrayCollection event handlers to validate the class of any item added to the ArrayCollection? Or does this cause problems??

      ...
      // set high priority to prevent other handlers being notified when invalid data added
      myCollection.addEventListener( CollectionEvent.COLLECTION_CHANGE, collectionChangeHandler, false, 1, true );
      ...

      private function collectionChangeHandler( event : CollectionEvent ) : void
      {
      if( event.kind == CollectionEventKind.ADD )
      {
      var newItem : Object = myCollection.getItemAt( event.location );
      var newMyClass : MyClass = newItem as MyClass;
      if( newMyClass == null )
      {
      myCollection.removeItemAt( event.location );
      throw new Error( "myCollection can only accept items of type MyClass" );
      }
      }
      }