Event Dispatching RL1 vs RL2

roelof's Avatar

roelof

30 Sep, 2015 05:20 PM

Hi,

I am porting a test startup project from RL1 to RL2. Up till now everything went ok, but I am getting errors on my Event Dispatching lines.

In RL1 I used the following commands:
dispatch(new SomeEvent(SomeEvent.DONE));
and
dispatchEvent(new SomeEvent());

but in RL2 I had to change my code to include

[Inject]
public var eventDispatcher:IEventDispatcher;

and I have to change the code to:
eventDispatcher.dispatchEvent(new SomeEvent(SomeEvent.DONE));
and
eventDispatcher.dispatchEvent(new SomeEvent());

Is this correct, because honestly, I find the documentation for RL2 confusing and incomplete.

Regards
Roelof

  1. 1 Posted by ishaban.flash on 30 Sep, 2015 07:34 PM

    ishaban.flash's Avatar

    Hi roelof,

    afair it is correct solution make notification from commands, but i think in anyway it is better to dispatch event with properly type.

  2. Support Staff 2 Posted by Ondina D.F. on 30 Sep, 2015 08:54 PM

    Ondina D.F.'s Avatar

    Hi Roelof,

    I can't help you much at the moment, because I'm on holiday...
    In addition to ishaban's answer just a short remark:

    rl1 commands looked like so:
    https://github.com/robotlegs/robotlegs-framework/blob/version1/src/...

    And an Actor class like this:
    https://github.com/robotlegs/robotlegs-framework/blob/version1/src/...

    If you like the convenience classes from rl1, you can build your own command and actor base classes and use them with rl2.

    Ondina

  3. 3 Posted by roelof on 01 Oct, 2015 05:39 AM

    roelof's Avatar

    Thanks for the reply, I will study the links that you provided Ondina and take it from there. I just wanted to make sure that I am on the right track, because there is still a lot I have to learn about AS3 / Flex / RL

  4. Support Staff 4 Posted by Ondina D.F. on 08 Oct, 2015 03:09 PM

    Ondina D.F.'s Avatar

    Hi Roelof,

    I'm back. Sorry for the delay.

    The Command class in rl2 has been intentionally kept lightweight. All is needed for a Command to be recognized as such by rl2 is to expose a public method execute().
    The rl1 Command had injections for the contextView, injector, mediatorMap, commandMap and eventDispatcher. Those came in handy, because we used to do the mappings inside of commands. With rl2 this is no longer necessary. You still can perform your mappings in commands, if you want. However, a class implementing IConfig is a much better way of structuring your mappings. You can inject whatever you need (and only what you need) into such a config class. This way they are much more lightweight. If you need help with config classes, let me know. Here just a link to a discussion about config classes:
    http://knowledge.robotlegs.org/discussions/robotlegs-2/12476-rl2-bo...

    So, as I said in my previous post, you can create a base class that contains the injections you need.
    Just for having access to the shared event dispatcher and to be able to use the same syntax as with rl1, you can do this:

    package yourdomain.commons.baseClasses
    {
        import flash.events.Event;
        import flash.events.IEventDispatcher;
        
        public class SharedDispatcher
        {
            [Inject]
            public var eventDispatcher:IEventDispatcher;
            
            
            protected function dispatch(e:Event):void
            {
                if (eventDispatcher.hasEventListener(e.type))
                    eventDispatcher.dispatchEvent(e);
            }
        }
    }
    

    Then you can let your commands, models and services extend the SharedDispatcher.

    If you want a rl2-command to behave exactly like a rl1-command:

    package yourdomain.commons.baseClasses
    {
        import flash.events.Event;
        import flash.events.IEventDispatcher;
    
        import robotlegs.bender.extensions.eventCommandMap.api.IEventCommandMap;
        import robotlegs.bender.extensions.mediatorMap.api.IMediatorMap;
        import robotlegs.bender.framework.api.IInjector;
    
        public class BaseCommand
        {
            [Inject]
            public var eventDispatcher:IEventDispatcher;
    
            [Inject]
            public var commandMap:IEventCommandMap;
    
            [Inject]
            public var injector:IInjector;
    
            [Inject]
            public var mediatorMap:IMediatorMap;
    
            public function BaseCommand()
            {
            }
    
            /**
             * @inheritDoc
             */
            public function execute():void
            {
            }
    
            /**
             * Dispatch helper method
             *
             * @param event The <code>Event</code> to dispatch on the <code>IContext</code>'s <code>IEventDispatcher</code>
             */
            protected function dispatch(event:Event):Boolean
            {
                if (eventDispatcher.hasEventListener(event.type))
                    return eventDispatcher.dispatchEvent(event);
                return false;
            }
        }
    }
    

    In this case, your commands will have to extend the BaseCommand and to override the public function execute().
    There is no need for a command extending the SharedDispatcher (give it a name of your choosing) to override execute()!

    If you don't want to use base classes, you're right, you have to inject the event dispatcher into your commands as you did in your post. Or in a Model or Service for that matter.

    Let me know if there is anything else you need help with while porting your project to rl2.

    By the way, can you tell us why are you doing something like this:

    dispatchEvent(new SomeEvent());

    I know that this works if you create the custom event class without a type argument, but mapping such an event to a command looks kind of weird with "" instead of a type ;)
    So, I'm with ishaban on this, I prefer using meaningful event's types .

    Ondina

  5. Ondina D.F. closed this discussion on 30 Nov, 2015 04:07 PM.

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

Keyboard shortcuts

Generic

? 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