ModuleContext eventDispatcher different from parent context
I am trying to let my child context listen to event dispatched
by the main context. Only I am having trouble to get this going
with the Joel's ModuleContext-class. Only the
ModuleContext -class isn't taking
eventDispatcher from the parent context but creates a
new one. I am not sure why. For example, if I check if the event
dispatcher has any listeners for the given event type it keep
returning false.
override public function startup():void
{
trace("eventDispatcher=" + eventDispatcher + " -> " + eventDispatcher.hasEventListener( MyEvent.EVENT ) );
trace("moduleDispatcher=" + moduleDispatcher + " -> " + moduleDispatcher.hasEventListener( MyEvent.EVENT ));
}
In the view associated with the context I am doing the following:
[Inject]
public function set parentInjector(value:IInjector):void
{
var ev: IEventDispatcher = value.getInstance(IEventDispatcher);
trace("MyModule.parentInjector=" + value + " -> " + ev + " -> " + ev.hasEventListener(MyEvent.EVENT) );
context = new ChildContext( this, value );
}
Here the hasEventListener command will correctly
return true but not in the first example there it will
return false twice. I thought the ModuleContext would
take over the mappings from parent injector here my main
context?
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
1 Posted by Weyert on 12 May, 2011 02:04 PM
The only way I am currently getting it working is making a static refer to the main context:
2 Posted by Stray on 12 May, 2011 02:04 PM
The norm is that the moduleDispatcher is shared, but the eventDispatcher is unique to each context, so I think you've just got a misunderstanding of how it works.
3 Posted by Weyert on 12 May, 2011 02:17 PM
Well, moduleDispatcher also says it doesn't have any event listeners for the event type. I am interested in.
The view gets created in a main application command like:
In the startup()-method of the main context I have:
Not sure, what I am doing wrong.
All the other mappings from the parent injector are getting fulfilled. Let's see if I am changing the mapping of IEventDispatcher somewhere
4 Posted by Weyert on 13 May, 2011 08:24 AM
Looks like it's not possible to get the parent event dispatcher in a ModuleContext. I will need to redispatch all events from main context event dispatcher over the module event dispatcher. Prone for errors :(
5 Posted by Stray on 13 May, 2011 08:29 AM
To be honest that's how I do it anyway - far from being 'prone for errors' I think it's a real aid to understanding and simplicity to use your module mediator as a relayer - there are even sugar functions for this purpose.
It means that just by looking in the module mediator you can instantly see what events are flowing in and out of this module.
It also makes it possible to develop your module as a stand-alone unit of functionality, and test it as such.
I think it's a win, not a lose. A small amount of additional boilerplate vs potentially hard-to-diagnose problems.
If the sugar functions mapRedispatchToModules and mapRedispatchInternally aren't on Joel's fork (in module mediator) then you can get them from mine. One line of code maps the process - I don't think I ever make errors with this (except forgetting to do it - which I cover with tests).
Stray
6 Posted by Weyert on 13 May, 2011 08:48 AM
I can understand it's the better approach. But if I want to use the ModuleMediator class I need to fulfil a bunch of injector mappings in the main context like IModuleCommandMap that can't be right.
I will have a closer look at the examples on Github. I seriously have to be doing something wrong.
7 Posted by Weyert on 13 May, 2011 09:00 AM
I will drop this module stuff or now seems painful to work with (as in I dont have time to get this working)
Stray closed this discussion on 07 Jun, 2011 01:33 PM.