Thanks guys.
Can someone explain how ac:Observe (which this is based on) works? i.e. what
triggers the handler call when somethng in the middle of the source binding
expression changes (eg. "b" in "{a.b.c.myCollection}"? I don't see any event
handlers, and there's a mysterious execute() method in the parent class
(Observer).
And would your version below also fire when b changes?
Otherwise, Josh's example works too, but the collection event is the only
trigger.
On Thu, Aug 14, 2008 at 12:12 AM, Dennis van Nooij <[EMAIL PROTECTED]>wrote:
> try this:
>
> package com.adobe.ac
> {
> import mx.events.CollectionEvent;
> import mx.collections.ArrayCollection;
> import flash.events.Event;
> import mx.core.Application;
> import mx.core.UIComponent;
>
>
> /**
> *
> * monitors Collections and react on reassining of the variable
> * and changes in the collection which are bindable
> *
> */
> public class ObserveCollection extends Observer
> {
> private var _handler : Function;
> private var _source : Object;
>
> override public function get handler() : Function
> {
> return _handler;
> }
>
> public function set handler( value : Function ) : void
> {
> _handler = value;
> if( value != null )
> {
> isHandlerInitialized = true;
> if( isHandlerInitialized && isSourceInitialized )
> {
> callHandler();
> }
> }
> }
>
> override public function get source() : Object
> {
> return _source;
> }
>
> public function set source( value : Object ) : void
> {
> if (_source != null){
>
>
> _source.removeEventListener(CollectionEvent.COLLECTION_CHANGE,collectionChangeHandler);
>
> }
> _source = value;
>
>
> _source.addEventListener(CollectionEvent.COLLECTION_CHANGE,collectionChangeHandler);
>
> isSourceInitialized = true;
>
> if( isHandlerInitialized && isSourceInitialized )
> {
> callHandler();
> }
> }
>
> protected override function callHandler() : void
> {
> try
> {
> handler.call( null, source );
> }
> catch( e : Error )
> {
> delay( e );
> }
> }
>
> protected override function delay( e : Error ) : void
> {
> UIComponent( Application.application ).callLater( throwException, [
> e ] );
> }
>
> private function throwException( e : Error ) : void
> {
> throw e;
> }
>
> private function collectionChangeHandler (event:Event) : void
> {
>
> callHandler();
> }
>
>
> }
> }
>
> cheers,
> Dennis
>
>
> --- In [email protected] <flexcoders%40yahoogroups.com>, "Alex
> Harui" <[EMAIL PROTECTED]> wrote:
> >
> > Never used Observe, but if it implements IMXMLObject or you subclass and
> > do so, then you can use it in MXML
> >
> >
> >
> > ________________________________
> >
> > From: [email protected] <flexcoders%40yahoogroups.com> [mailto:
> [email protected] <flexcoders%40yahoogroups.com>] On
> > Behalf Of Richard Rodseth
> > Sent: Wednesday, August 13, 2008 4:16 PM
> > To: [email protected] <flexcoders%40yahoogroups.com>
> > Subject: [flexcoders] Observing collections
> >
> >
> >
> > For some reason I have an aversion to adding event listeners in
> > ActionScript, favouring binding expressions and the Observe tag from
> > Adobe Consulting. Not sure how rational this is, but there you have it.
> > Binding is just so damn elegant.
> >
> > However, collections are a problem. It seems that so often you want to
> > update something if either the collection itself or its contents
> > changes, and you don't really care about the details of the change.
> >
> > I suppose if you're a DataGrid watching its dataprovider you do care and
> > can minimize updates, but in many of the use cases I've encountered,
> > that's not the case - my observer is going to do something with the
> > whole collection (or maybe I've just been lazy about exploiting possible
> > optimizations).
> >
> > Is there anything like the Observe tag that can be instantiated in MXML
> > and can trigger a function call on a COLLECTION_CHANGE event?
> >
>
>
>