views:

44

answers:

3

I am having trouble figuring out how to build a console for my application so i can send msgs to it from any class in the package.

i tried to send a generic Event, but I cannot find a way send a msg together with the event, or at least a reference to the object which is dispatching the event.

what approach would you suggest?

A: 

The easiest way is to create a DynamicEvent class that you can add any message you want to it.

package {
    import flash.events.Event;
    dynamic public class DynamicEvent extends Event {
        public function DynamicEvent(t:String, b:Boolean = false, c:Boolean = false){
            super(t, b, c);
        }
    }
}

By setting this class to be dynamic (using dynamic keyword) you are able to add values to your event object:

var _dynEvent:DynamicEvent = new DynamicEvent(LOAD_PROGRESS);
_dynEvent.id = _id;
_dynEvent.bytesLoaded = bytesLoaded;
dispatchEvent(_dynEvent);

Then when you listen for the event:

function eventHandler(e:DynamicEvent):void{
    trace(e.bytesLoaded);
    trace(e.id);
}
danjp
You might need to override the clone() method of the Event too -- see http://www.bit-101.com/blog/?p=1143. This could be tricky with dynamic events.
Cameron
@Cameron: you can grab all dynamic properties in a for-in loop.@danjp: wouldn't do it that way. rather have a property `data`of type `*` store all data passed with the event. this way there is no conflict with `Event`'s properties (such as `type` or `target`).
back2dos
@Cameron @back2dos - yeah you're both right. This is easy and simple though, (read 'quick and dirty') and although I wouldn't do this for any large projects, there is a good chance it would be ok for this one.
danjp
+3  A: 

A custom event with the dynamic keyword will work as advertised in Danjp's post, but I'd advise against it on any sort of larger project. You lose the ability to strictly type what your informational payload is - if someone else has to edit your code, they may have no idea that you've got a dynamically generated "myData" property on your event, for instance. It will cause no end of headaches for someone trying to edit your code later.

The "correct" way to do this is to create custom events with strongly typed members that hold your data. So for instance:

package com.yourdomain.events {
    import flash.events.Event;
    import com.yourdomain.model.vo.MyValueObject;

    public class MyEvent extends Event {
        public var myVO:MyValueObject;
        public function MyEvent(type:String):void {
            super(type);
        }
    }
}

So, yeah - that's kind of it in a nutshell. You'll want to google the exact format, mine is simplified - your super() call should have other parameters, and you'll want to override the clone() method. But the advantage here is that anyone inspecting your code knows EXACTLY what kind of datatype to look for as a payload. In this case, it's an instance of MyValueObject.

Let me know if you have any specific questions about why, but that's the general idea. Whenever you do a large AS3 project you'll want to use custom events like this to not only dispatch various notifications but also to carry clearly defined and strictly-typed data payloads.

Myk
pls, look at the code i posted. i am still confused
vasion
A: 

I tried what you guys suggested.. this is my event code

public class ConsoleEvent extends Event
{
    public var variable: String;
    public function ConsoleEvent( variable1: String, type:String='message',bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(type, bubbles, cancelable);
        this.variable = variable1;
    }
    override public function clone():Event
    {
        return new ConsoleEvent ( variable,type, bubbles, cancelable);
    }

}
}

but this is the trace

[Event type="message" bubbles=false cancelable=false eventPhase=2]

i have caught the event with

reader.addEventListener('message', consoleadd);
vasion
Yeah, you're doing fine - in your "consoleadd" function, instead of saying "trace(event)" say "trace(event.variable)". What you have there is a trace of an actual event object, it's giving you all of the standard event information. If you want, you can override public function toString():String in your custom event to trace out the variable in addition to type, bubbles, cancelable and phase. If that makes sense.But yeah, event.variable in your handler will give you what you want!
Myk