Hi all,
I'm in the process of converting a quite extensive code framework
from AS2 to AS3, and I've got a 'how do you cope with this situation?'
question.
I understand events, event listeners, EventDispatcher etc., but I'm
missing a trick somewhere (probably from my historical use of Delegate
with additional params).
Say we have object A, which supports the following:
A.addEventListener(type:String,handler:Function);
A.loadSomething(url:String); // Dispatches Event.COMPLETE when done
and object B wraps it and wants to do something context specific once
A has finished loading:
(pseudo-AS3!)
class B
{
:
function addEventListener(/as per usual/);
// Dispatches MyEvent.ITSOVERWITH when done
function loadSomethingAndProcessIt(url:String,contextInfo:Object):void
{
var a:A=new A();
a.addEventListener(Event.COMPLETE,onALoaded);
a.loadSomething(url);
}
function onALoaded(event:Event):void
{
var a:A=A(event.target);
// Process the results of A being loaded
// : blah
// then:
dispatchEvent(new MyEvent(MyEvent.ITSOVERWITH,someDataIGotFromA));
}
}
It doesn't really matter what events either are firing; what does matter is that
calling loadSomethingAndProcessIt() is both asynchronous and atomic
i.e. multiple loadSomethingAndProcessIt() calls could be running at
the same time on the same B.
What I'm trying to do is to find a sane, 'generally accepted' way to
get information from loadSomethingAndProcessIt() to onALoaded()
without A being aware of it at all. So to access the contextInfo
variable from within onALoaded().
What I'd have done in AS2 is to use an extended Delegate (or Proxy) -
the listener would have been:
a.addEventListener(Event.COMPLETE,Delegate.create(this,onALoaded,contextInfo));
Which works fine.
Now, I've written the equivalent of that sort of Delegate/Proxy for
AS3, and it works perfectly well and does the trick. However, that
method appears to be Frowned Upon by the community, for reasons which
escape me. So I guess my question is:
- Why, in AS3, is my equivalent of using Delegate Not The Done Thing?
(Someone mentioned type-safety; and yes, at compile time, you'll lose
type safety)
- How else would you do it? What's the 'standard' approach?
This situation comes up a lot - really, a huge number of times - in
our code frameworks. This may be because they take the wrong approach
- perhaps I can't see the wood for the trees.
For clarity, we can't just store contextInfo inside B, as there may be
multiple requests. We could conceivably store a look up table in B
with various contextInfos tied to varies requests to A, but that's
just overkill.
Thoughts?
Ian
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com