tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/4493-dispatching-context-initialization-complete-with-common-eventdispatcherRobotlegs: Discussion 2013-08-07T15:03:57Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/281315932013-08-06T16:36:17Z2013-08-06T16:48:19ZDispatching Context initialization complete with common EventDispatcher<div><p>Hi Jakub,</p>
<p>I see 3 solutions to this:</p>
<p><strong>a.</strong> the one you mentioned, injecting the
dispatcher into a class that implements IConfig</p>
<p><strong>b.</strong> in an afterInitialize handler let the
injector get an instance of the IEventDispatcher</p>
<p><strong>c.</strong> mediate the main View and let the Mediator
dispatch the event</p>
<p>The class that creates the context can't be injected with
anything, simply because the Injector is not available yet and
neither is the shared IEventDispatcher.</p>
<p>I'm sure you know what happens when a context is created, but I
thought I'd mention the steps just to make my point clearer and
also for the benefit of other users who don't know as much as you
do.</p>
<pre>
<code>[1] _context = new Context()
[2] .install(MVCSBundle)
[3] .configure(MediatorsConfig, ControllersConfig, ModelsConfig, ServicesConfig)
[4] .configure(new ContextView(view));</code>
</pre>
<p>After creating a context:</p>
<p><strong>[1]</strong> _context = new Context();</p>
<p>you have access to an Injector.</p>
<p>But, such a _context is pretty useless without the extensions
provided by the MVCS Bundle.</p>
<p>After running this:</p>
<p><strong>[2]</strong> _context.install(MVCSBundle)</p>
<p>the extensions are installed, and so the
EventDispatcherExtension is available, and the Injector could get
an IEventDispatcher instance for you, if you wanted it to.</p>
<p>But, the _context is still not very effective at this stage and
your application even less. All the Mediators, Commands, Models and
what else need to be wired together.<br>
A good thing about the IConfig interface is that you can split up
the bootstrapping process into several classes (implementing
IConfig):</p>
<p><strong>[3]</strong> _context.configure(MediatorsConfig,
ControllersConfig, ModelsConfig, ServiceConfig)</p>
<p>At this stage the config classes are queued and the context only
needs one more thing for it to be of real use, and that is the
contextView:</p>
<p><strong>[4]</strong> _context.configure(new
ContextView(view));//view = DisplayObjectContainer</p>
<p>This triggers the initialization process. First now the queued
config classes are instantiated and the mappings performed inside
of configure().<br>
configure() will be invoked after dependencies have been supplied.
That means that an injected IEventDispatcher will be available by
the time configure() runs, but it wouldn't make much sense to
dispatch an event at this stage, especially because of possible
race conditions.</p>
<p>Now let's go back to the 3 options mentioned in the beginning of
my post.</p>
<p><strong>a.</strong> add a ConfigComplete :</p>
<p>.configure(MediatorsConfig,ControllersConfig, ModelsConfig,
ServicesConfig , ConfigComplete)</p>
<pre>
<code>public class ConfigComplete implements IConfig
{
[Inject]
public var evenDispatcher:IEventDispatcher;
[Inject]
public var _context:IContext;
public function configure():void
{
_context.afterInitializing(afterInitializing);
}
private function afterInitializing():void
{
evenDispatcher.dispatchEvent(new SomeEvent(SomeEvent.SOME_REQUEST));
}
}</code>
</pre>
<p><strong>b.</strong></p>
<pre>
<code>private var _context:IContext;
public function ShellContext(view:DisplayObjectContainer)
{
_context = new Context()
.afterInitializing(afterInitializing)
.install(MVCSBundle)
.configure(MediatorsConfig, ControllersConfig, ModelsConfig, ServicesConfig)
.configure(new ContextView(view));
}
private function afterInitializing():void
{
var eventDispatcher:IEventDispatcher = _context.injector.getInstance(IEventDispatcher);
evenDispatcher.dispatchEvent(new SomeEvent(SomeEvent.SOME_REQUEST));
}</code>
</pre>
<p><strong>c.</strong> the mediator of the main view has access to
the shared event dispatcher in its initialize() method, which is
invoked after the context has finished the initialization
process.</p>
<p>I'd prefer option a -using a config class with an
afterInitializing() method.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/281315932013-08-06T18:36:18Z2013-08-06T18:36:18ZDispatching Context initialization complete with common EventDispatcher<div><p>Thank you very much Ondina. This the most comprehensive answer I
could<br>
imagine. I'd prefer option a too.</p>
<p>Thank you again.</p>
<p>Jakub</p></div>jakub.gondektag:robotlegs.tenderapp.com,2009-10-18:Comment/281315932013-08-07T15:03:54Z2013-08-07T15:03:54ZDispatching Context initialization complete with common EventDispatcher<div><p>You're welcome, Jakub! It's good to hear that my answer helped
you.</p></div>Ondina D.F.