Inject IEventDispatcher with name,doesn't work for Command

foodyi's Avatar

foodyi

08 Feb, 2014 09:39 AM

config

injector.map( IEventDispatcher,"model_to_view").toSingleton( EventDispatcher );
eventCommandMap.map( DocumentEvent.OPEN_DOCUMENT,DocumentEvent).toCommand( OpenDocumentCommand );
dispatch event
[Inject(name='model_to_view')]
public var dispatcher:IEventDispatcher;
dispatcher.dispatchEvent( new DocumentEvent(DocumentEvent.OPEN_DOCUMENT,document));

I thought OpenDocumentCommand won't be triggered, but i was wrong. my problem is how can i dispatch DocumentEvent.OPEN_DOCUMENT event and the command doesn't execute.

  1. Support Staff 1 Posted by Ondina D.F. on 08 Feb, 2014 01:19 PM

    Ondina D.F.'s Avatar

    Hello,

    Your question isn't very clear.

    [1] In the title of your post you said:

    Inject IEventDispatcher with name,doesn't work for Command

    [2] but then you said:

    thought OpenDocumentCommand won't be triggered, but i was wrong. my problem is how can i dispatch DocumentEvent.OPEN_DOCUMENT event and the command doesn't execute.

    [1] That doesn't work because the EventCommandMapExtension is using the context's default IEventDispatcher, so only events that were dispatched on that shared dispatcher will trigger a command.

    Currently, there is no way, I know of, of telling the EventCommandMap which dispatcher to use when mapping an event to a command.
    You could create an additional EventCommandMap, to which you pass your named dispatcher, and map IEventCommandMap with a name, so it doesn't override the mapping for the default one, to the value of your new EventCommandMap.

    Something like this:

    var someDispatcher:EventDispatcher = new EventDispatcher();
    injector.map(IEventDispatcher, "someDispatcher").toValue(someDispatcher);
    
    var specialCommandMap:EventCommandMap = new EventCommandMap(context, someDispatcher); 
    injector.map(IEventCommandMap, "specialCommandMap").toValue(specialCommandMap);
    
    specialCommandMap.map(SomeEvent.TRIGGER_SOMECOMMAND, SomeEvent).toCommand(SomeCommand);
    

    But, I think, this can become messy in case you need several named event dispatchers.

    [2] I don't understand this question. Are you asking why the command is triggered or why it isn't triggered?

    If you're asking why it is triggered despite the fact the event dispatcher is not the context's default event dispatcher, that can happen if you're dispatching the event from within a Mediator using dispatch()

    dispatch( new DocumentEvent(DocumentEvent.OPEN_DOCUMENT,document));

    dispatch () will trigger the command because a Mediator (https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...] is injected with the shared event dispatcher and when you call dispatch() it dispatches the event on that share event dispatcher:

    protected function dispatch(event:Event):void
    {
        if (eventDispatcher.hasEventListener(event.type))
            eventDispatcher.dispatchEvent(event);
    }
    

    If you're asking how to prevent the execution of the command when you dispatch an event to which the command has been mapped, you either:

    • unmap the command before dispatching the event

    commandMap.unmap(DocumentEvent.OPEN_DOCUMENT, DocumentEvent).fromCommand(OpenDocumentCommand)

    • set once to true = the command runs only once

    eventCommandMap.map( DocumentEvent.OPEN_DOCUMENT,DocumentEvent).toCommand( OpenDocumentCommand ).once(true);

    • or you simply use another event type, which is preferable, in case you need to trigger that command again

    If I didn't answer your question, please be more clear about what it is you want to achieve. :)

    Ondina

  2. 2 Posted by foodyi on 08 Feb, 2014 01:50 PM

    foodyi's Avatar

    sorry , my title made you confused.

    hmm, I want to create another shared EventDispatcher which will not dispatch event to command despite the event is mapping for command. so I Inject a EventDispatcher with name . when i use my custom EventDispatcher dispatch a mapping event, the command still execute, i don't understand that. i didn't dispatch event from mediator by dispatch function.

    in fact, i had considered use another event type, but not sure if it is a good way.
    really appreciate for your detailed answer, i got a lot of help.

  3. Support Staff 3 Posted by Ondina D.F. on 08 Feb, 2014 02:34 PM

    Ondina D.F.'s Avatar

    when i use my custom EventDispatcher dispatch a mapping event, the command still execute, i don't understand that.

    Well, that's impossible because of what I've explained in the first part [1]

    There must be something else going on. Can you paste the code for the method where you dispatch the event to trigger the command on the default event dispatcher? Maybe that function/method is called from somewhere else in your app for some reasons?

    in fact, i had considered use another event type, but not sure if it is a good way.

    Why is it not a good way? It seems you want to use your OPEN_DOCUMENT event for 2 different actions: one is for triggering the command, and the other for something else. Using another event type for the second action makes your intentions clear. What should happen when you dispatch OPEN_DOCUMENT on the custom event dispatcher? Who is listening for that event and what should that class do? I assume "model_to_view" dispatcher is injected into a Model, which dispatches the event on that custom dispatcher when its data changes, so it would be good to use an event type that is describing what just happened.

  4. 4 Posted by foodyi on 08 Feb, 2014 03:01 PM

    foodyi's Avatar

    There must be something else going on. Can you paste the code for the method ?>where you dispatch the event to trigger the command on the default event dispatcher? >Maybe that function/method is called from somewhere else in your app for some >reasons?

    you are right, I checked my code again , I had mapped a wrong event type for the command. Changing event type is better than create another shared eventDispatcher ,right ?

  5. Support Staff 5 Posted by Ondina D.F. on 08 Feb, 2014 04:18 PM

    Ondina D.F.'s Avatar

    Changing event type is better than create another shared eventDispatcher ,right ?

    Yes, I think it's better and also easier to implement.
    It is more efficient and more clear to have 2 different event types detonating 2 different actions than having 2 event dispatchers dispatching the same event type.

    In your scenario, if a Mediator is listening for OPEN_DOCUMENT event that is also mapped to a Command, you'd have to inject the named dispatcher and add an explicit listener to it.
    But, if you'd dispatch the event with another event type on the default dispatcher, the only thing you'd need to do would be :
    addContextListener(DocumentEvent.SOME_DATA_CHANGED, onSomeDataChanged, DocumentEvent);
    No need for named injections in the Model (or other class that is dispatching on the custom dispatcher) and Mediator anymore.

    So, your custom DocumentEvent should contain different constants for different actions..

  6. 6 Posted by foodyi on 09 Feb, 2014 12:51 AM

    foodyi's Avatar

    thank you so much, now i know how to deal this situation very well. i 'll I will continue to enjoy rl2. ^ ^

  7. Support Staff 7 Posted by Ondina D.F. on 10 Feb, 2014 11:36 AM

    Ondina D.F.'s Avatar

    You're welcome :)

  8. Ondina D.F. closed this discussion on 10 Feb, 2014 11:36 AM.

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