Event handler scope issue

Hello all,

I am having an issue with scope and an event handler… something I would have fixed in AS2 with the Delegate hack class. Here’s the scenario:

I am working on a LoadQueue class that is designed to return a custom object that represents a load job when added to queue. Here’s the relevant code from the class:


package
{
    public class LoadQueue ()
    {
        public function addToQueue (t:String, c:DisplayObject):Object
        {
            var queueObj = new Object();
            queueObj.target = t;
            queueObj.container = c;
            queueObj.loader = new Loader();
        
            queueObj.addEventListener = function (event:String, func:Function):void
            {
                this.loader.contentLoaderInfo.addEventListener(event, func);
            }
            
            var _url = new URLRequest(queueObj.target);
            queueObj.loader.load(_url);
            queueObj.container.addChild(queueObj.loader);
            
            return queueObj;
        }
    }
}

and here is how that code is implemented in the swf:


var target1 = new Sprite();
addChild(target1);

var queue = new LoadQueue();
var load1 = queue.addToQueue("images/image01.jpg", target1);

load1.addEventListener(Event.COMPLETE, onLoadComplete);

var onLoadComplete = function(event:Event):void
{
     trace(event.target);
}

I’ve created a function on the queueObj object to replicate the as3 event functionality - it is meant to work as though you were listening to the Loader.contentLoaderInfo events, but without having to explicitly assign the event handler to queueObj.loader.contentLoaderInfo.

The event triggers fine, but with one issue - event.target references the contentLoaderInfo object (as it should be!), but I want it to point to the queueObj instead of the contentLoaderInfo object. As I said, in AS2 I would have used the Delegate hack to work around this kind of scope issue… something like this:


//AS2.0 Delegate hack
queueObj.addEventListener = function (event:String, func:Function):void
{
    this.loader.contentLoaderInfo.addEventListener(event, Delegate.create(this, func));
}

but in AS3, I’m not sure how to approach the problem.

I was thinking something along the lines of the following would be the solution:


queueObj.addEventListener = function (event:String, func:Function):void
{
    this.loader.contentLoaderInfo.addEventListener(event, func.apply(this, [?????]));
}

but of course i need to pass the single argument (the Event object) that gets passed by the event dispatcher. How can i grab that object from the loader.contentLoaderInfo scope and apply it to the queueObj scope? Any ideas? feel free to ask for further clarification if any of this is confusing or nonsensical.

Thanks for your time and help,
-Eric