Listening for events from context in class that does not extend anything from RL

seanlail's Avatar


06 Jan, 2010 09:01 PM

That's probably a terrible subject line...

I've done a few small apps now using the Mediator pattern and now I'm exploring how to use RL without it.
I'm trying to have a View (just a Sprite)... then inject some dependencies into it... and then have it handle events from Commands / Actors to update itself.

Injections are simple, I just mapped them in my context with "viewMap.mapType".

When I dispatch an event from an Actor or Command though, I can't handle it in the View.
Simply adding a listener for the event obviously doesn't work, as I need to listen to the context and my view is not aware of the context..

How do I get around this?
Do I need to Inject a singleton-esque instance of the Context then?
Wouldn't that create more issues though?



  1. 1 Posted by levi.strope on 06 Jan, 2010 10:14 PM

    levi.strope's Avatar

    Hey bud.

    I think I understand what you are trying to do. I'll throw it in my own words:

    • You have a context that maps all your views/models/command dependencies
    • You have classes that extend Actor and are dispatching events from these classes
    • You have a view that instantiates the context once the view arrives on stage
    • You have no mediators

    Now your problem in my words:

    • You Dispatch event Z from actor B. You need the view to act upon event Z accordingly but you are unable to add event listeners and all that jazz because your view does not extend anything RL.

    Here is how I would do it:

    For each event that your view needs to act upon, I would create a corresponding command. So if your actor makes an update to your model and dispatches a property update event, you will listen for this even in the Context.

    Within your command class, just inject your view object into the command or add it as the payload to the event.

    Now once your command class has a reference to the view, you should be able to alter the view accordingly within the command's execute function.

    Essentially just take every method you would have written inside of a Mediator and make it a Command. Don't forget that each time you do this you will add another listener to the context.

  2. 2 Posted by levi.strope on 06 Jan, 2010 10:17 PM

    levi.strope's Avatar

    A side note to this conversation:

    The more logic you put into commands - the easier your application will be to unit test. Commands are far easier to test when you don't have to mock a view component, but in your situation would still need to mock the view.

    If you had mediators you would only have to mock views for mediators. Still - you can make it easier on yourself if you leave as much to the controller (commands) as possible.

  3. Support Staff 3 Posted by Shaun Smith on 06 Jan, 2010 10:24 PM

    Shaun Smith's Avatar

    Or, simply add this to your view class:

        public var eventDispatcher:IEventDispatcher;

    As a side note: There is nothing special about any of the RL actor type classes - they simply set up commonly used dependencies. If you don't want to extend them you just need to set up the injection points manually. When in doubt, have a peek at the RL source for these classes, such as:

    Side note 2: EventMaps don't get injected - they are usually created locally through a lazy getter, like so:

  4. 4 Posted by seanlail on 07 Jan, 2010 12:45 AM

    seanlail's Avatar

    Brilliant, thanks guys.
    I like the simplicity of injecting the eventDispatcher.

    I'm just playing around with different ways to build things at the moment to see what the best way for daily development would be.

    Thanks again.

  5. Till Schneidereit closed this discussion on 02 Mar, 2010 12:55 PM.

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

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac