Dispatch event from custom class

oscar's Avatar

oscar

11 Mar, 2010 05:13 AM via web

Hi,

I working on an application that has some custom classes written in actionscript. Several of those classes are subclasses of other abstract classes. I need to dispatch a event from one of those classes. The dispatch() method is not available of course, I tried using the regular dispatchEvent() but looks like the event is not been listened by the command I mapped it to.

Any ideas will be appreciated.

Tks.

  1. 2 Posted by Mike Cann on 11 Mar, 2010 08:07 AM

    Mike Cann's Avatar

    Hi Oscar,

    I believe IEventDispatcher is injected into Actors, Mediators and Commands:

    http://github.com/robotlegs/robotlegs-framework/blob/master/src/org...

    So in your custom class you should beable to just do:

    [Inject] public var dispatcher : IEventDispatcher;

    and that should, be injected.

    Cheers,
    Mike

  2. 3 Posted by Jonny Reeves on 11 Mar, 2010 09:59 AM

    Jonny Reeves's Avatar

    Oscar,

    The reason your even was not being listened to in the CommandMap is because RobotLegs shared a common EventDispatcher which is created in the constructor of ContextBase.

    In order for your custom classes to talk to the rest of your RobotLegs application, you will have to give them reference to this EventDispatcher object (aka the Event Bus).

  3. Support Staff 4 Posted by Joel Hooks on 11 Mar, 2010 04:47 PM

    Joel Hooks's Avatar

    Jonny and Mike are correct, your class needs to have the IEventDispatcher that is used by the Context. I do this in various ways, you can use the [Inject] metadata. If you take this route you need to either have the injector create the instance of your custom class:

    injector.instantiate(MyClass);
    

    or have the injector inject into the class:

    injector.injectInto(myClassInstance);
    

    OR map the class and make it available for injection:

    injector.mapSingleton(MyClass);
    
    //somewhere else
    
    [Inject]
    public var myClassInstance:MyClass;
    

    ANOTHER approach is to have the IEventDispatcher as a constructor argument of your custom class and pass it in when you create it.

  4. 5 Posted by oscar on 11 Mar, 2010 04:59 PM

    oscar's Avatar

    Thanks guys for your response, that helps.

    One more question. Some of the custom classes will have several instances in the application. what is the best way to inject the IEventDispatch in this case?

    tks.

  5. 6 Posted by Jonny Reeves on 11 Mar, 2010 05:07 PM

    Jonny Reeves's Avatar

    Oscar, well that's an age old architecture problem - the sort of things RobotLegs was created to help solve ;)

    Like Joel mentioned, passing a reference to the RobotLegels IEventDispatcher in your custom classes constructor would be the usual approach.

  6. 7 Posted by oscar on 11 Mar, 2010 05:45 PM

    oscar's Avatar

    So, it makes sense to not use custom classes that aren;t views, mediators or models, right?

    Here's what I need to do.
    I'm have a view called Holder with its mediator HolderMediator. Holder will have children added at run time. Those children were my custom classes. If I want those children to implement RL, each one should have its own mediator and be able to dispatch events, etc.. How do I create the mediatopMap for each one of those children if they are added at run time? if the holder may have several instances of a child and I don't know the number of children (or type) the Holder will have.

    Thanks.

  7. Support Staff 8 Posted by Stray on 11 Mar, 2010 06:03 PM

    Stray's Avatar

    Hi Oscar,

    As long as you map them in your Context:

    mediatorMap.mapView(ChildViewClass, ChildViewMediatorClass);
    mediatorMap.mapView(OtherChildViewClass, OtherChildViewMediatorClass);
    

    then robotlegs will automagically take care of instantiating a mediator for every one of those views you instantiate, as soon as it is added to the stage.

    It doesn't matter whether you have 1 instance of ChildViewClass or 100 - as long as they're on the display list they'll be mediated.

  8. 9 Posted by oscar on 11 Mar, 2010 06:46 PM

    oscar's Avatar

    Thanks Guys, for all your help. I'm getting started with R.L. and it's just amazing.

Comments are currently closed for this discussion. You can start a new one.