Dispatching Context initialization complete with common EventDispatcher

jakub.gondek's Avatar

jakub.gondek

06 Aug, 2013 12:59 PM

Hi,

I'm trying to dispatch that my Context was fully initialized on the common EventDispatcher so it can be mapped to command. Have tried multiple takes but none of them seemed to work.

I've extended the Context, setted afterInitializing callback to call dispatchEvent or injected eventDispatcher.dispatch. dispatchEvent didn't call commands, eventDispatcher in Context was not injected so it couldnt dispatch anything.

I need that to process further app initialization (calling service methods to get data).

The only idea left is to add Config that dispatches the event, but that doesn't seem right to me. Is there a better way or is it just my prejudice and this is a good idea.

Best regards
Jakub Gondek

  1. Support Staff 1 Posted by Ondina D.F. on 06 Aug, 2013 04:36 PM

    Ondina D.F.'s Avatar

    Hi Jakub,

    I see 3 solutions to this:

    a. the one you mentioned, injecting the dispatcher into a class that implements IConfig

    b. in an afterInitialize handler let the injector get an instance of the IEventDispatcher

    c. mediate the main View and let the Mediator dispatch the event

    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.

    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.

    [1]  _context = new Context()
    [2] .install(MVCSBundle)
    [3] .configure(MediatorsConfig, ControllersConfig, ModelsConfig, ServicesConfig)
    [4] .configure(new ContextView(view));
    

    After creating a context:

    [1] _context = new Context();

    you have access to an Injector.

    But, such a _context is pretty useless without the extensions provided by the MVCS Bundle.

    After running this:

    [2] _context.install(MVCSBundle)

    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.

    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.
    A good thing about the IConfig interface is that you can split up the bootstrapping process into several classes (implementing IConfig):

    [3] _context.configure(MediatorsConfig, ControllersConfig, ModelsConfig, ServiceConfig)

    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:

    [4] _context.configure(new ContextView(view));//view = DisplayObjectContainer

    This triggers the initialization process. First now the queued config classes are instantiated and the mappings performed inside of configure().
    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.

    Now let's go back to the 3 options mentioned in the beginning of my post.

    a. add a ConfigComplete :

    .configure(MediatorsConfig,ControllersConfig, ModelsConfig, ServicesConfig , ConfigComplete)

    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));
        }
    }
    

    b.

    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));
    
    }
    

    c. 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.

    I'd prefer option a -using a config class with an afterInitializing() method.

    Ondina

  2. 2 Posted by jakub.gondek on 06 Aug, 2013 06:36 PM

    jakub.gondek's Avatar

    Thank you very much Ondina. This the most comprehensive answer I could
    imagine. I'd prefer option a too.

    Thank you again.

    Jakub

  3. Support Staff 3 Posted by Ondina D.F. on 07 Aug, 2013 03:03 PM

    Ondina D.F.'s Avatar

    You're welcome, Jakub! It's good to hear that my answer helped you.

  4. Ondina D.F. closed this discussion on 07 Aug, 2013 03:03 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