Dispatching event from AppConfig to start database configuration

vishwas.gagrani's Avatar

vishwas.gagrani

23 Feb, 2013 05:32 PM

Following some examples in RL 1.0, i used the following line of code to start the application from the configuration of database

dispatchEvent(new DatabaseEvent( DatabaseEvent.CONFIGURE));

I used above line but it's giving error in RL2.0 .
So, I referred some of the examples listed at ( http://knowledge.robotlegs.org/discussions/examples/20-links-to-rob...). What i followed is either now i need to inject eventdispatcher ( in AppConfig.as ? ) , or i should mediate the main-view, that would dispatch the required event.

Or should i use context to dispatch event as follows ( However I couldnot make it work ):

context.dispatchEvent(new DatabaseEvent( DatabaseEvent.CONFIGURE));

PS: I also found something as follows, seems like what i am looking for :

    `context.lifecycle.afterInitializing( init );`

Or is their some other better way ? I need some guidance on this.
Thnx
V.

  1. 1 Posted by zuzu on 23 Feb, 2013 10:52 PM

    zuzu's Avatar

    I am still new to RL2 (currently converting an old half-finished RL1 project) so I am no expert, but the most common way to dispatch Events in RL2 seems to be by injecting the eventDispatcher into the class:

    [Inject]
    public var dispatcher:IEventDispatcher;
    

    and use it like this:

    dispatcher.dispatchEvent(new DatabaseEvent( DatabaseEvent.CONFIGURE));
    

    You can dispatch events (almost?) wherever you want but if you want to do it right after the mapping is done it could look somewhat like this:

    public class MyAppConfig implements IConfig {
        [Inject]
        public var context:IContext;
    
        [Inject]
        public var dispatcher:IEventDispatcher;
    
        public function configure():void { 
            //do your mappings here
    
            context.afterInitializing(init);
        }
    
        private function init():void { 
            dispatcher.dispatchEvent(new DatabaseEvent( DatabaseEvent.CONFIGURE));
        }
    }
    

    Hope that helps
    ~Christina

  2. 2 Posted by vishwas.gagrani on 24 Feb, 2013 05:31 AM

    vishwas.gagrani's Avatar

    thnx Christina.

    Btw.. I am getting this error in the above :
    Injector is missing a mapping to handle injection into target "[class AppConfig]" of type "swc::AppConfig".

    V.

  3. 3 Posted by zuzu on 24 Feb, 2013 11:26 AM

    zuzu's Avatar

    The above code is compiling for me. The error message makes me believe context and dispatcher could not be injected into the class, hinting a problem with how you set up your context.

    A minimal setup may look like this:

    _context = new Context()
        .install(MVCSBundle)
        .configure(MyAppConfig, new ContextView(this));
    

    The MVCS Bundle is not needed to get my code above to run, but as soon as you want to map mediators/commands and so on you need either the bundle or install all needed extensions yourself.

  4. 4 Posted by vishwas.gagrani on 24 Feb, 2013 12:10 PM

    vishwas.gagrani's Avatar

    Got rid of that error, but I am still not able to dispatch any event. Here are my 3 blocks of code Main.as ( Document Class), AppConfig.as, controller/ConfigureDatabaseCommand

    Main.as

    package app 
    { 
        import swc.AppConfig;
        import flash.display.Sprite ;
        import flash.events.Event;
        import robotlegs.bender.framework.api.IContext;
        import robotlegs.bender.framework.impl.Context;
        import robotlegs.bender.extensions.contextView.ContextView;

    [Frame(factoryClass = "app.Preloader")]
    public class Main extends Sprite 
    {
        protected var _context:IContext;
        public function Main()
        {
            trace("Main");
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
    
        }
    
        public function init(e:Event=null):void
        {
            _context = new Context();
            _context.configure(AppConfig,new ContextView( this ));
        }
    }
    
    
    
    
    }

    AppConfig.as :

    package swc
    {
        
    import robotlegs.bender.framework.api.IConfig;
    import robotlegs.bender.extensions.mediatorMap.api.IMediatorMap;
    import robotlegs.bender.extensions.eventCommandMap.api.IEventCommandMap;
    import robotlegs.bender.extensions.contextView.ContextView;
    import swc.controller.ConfigureDatabaseCommand;
    import swc.controller.MapContainerCommands;
    import swc.view.StartMenuMed;
    import swc.view.StartMenuView;
    import flash.display.DisplayObjectContainer;
    import swc.model.StatsModel;
    import swc.services.events.DatabaseEvent;
    import swc.services.XMLLoaderService;
    import flash.events.IEventDispatcher ;
    import robotlegs.bender.framework.api.IContext;     
    import robotlegs.bender.framework.api.ILogger;
    import robotlegs.bender.framework.api.LogLevel;
    import org.swiftsuspenders.Injector;
    import robotlegs.bender.bundles.mvcs.MVCSBundle;
    
        public class AppConfig implements IConfig
        {
    
           private var commandMap:IEventCommandMap;
                
            [Inject]
            public var mediatorMap:IMediatorMap;
            [Inject]
            public var injector:Injector;
            [Inject]
            public var context:IContext;
            [Inject]
            public var logger:ILogger;
            [Inject]
            public var dispatcher:IEventDispatcher;
            [Inject]
            public var contextView:ContextView;
    
            
            public function AppConfig(rootDisplayObject:DisplayObjectContainer) 
            {
            }
            
            public function configure():void 
            {
                context.install( MVCSBundle);
                context.logLevel = LogLevel.DEBUG;
                logger.info( "configuring application" );
                mediatorMap = context.injector.getInstance(IMediatorMap);
                mediatorMap.map(StartMenuView).toMediator(StartMenuMed);
                commandMap = context.injector.getInstance(IEventCommandMap);
                commandMap.map(DatabaseEvent.CONFIGURE, DatabaseEvent).toCommand(ConfigureDatabaseCommand);
                    context.injector.map(StatsModel).asSingleton();
                context.injector.map(XMLLoaderService).asSingleton();
                dispatcher.dispatchEvent(new DatabaseEvent( DatabaseEvent.CONFIGURE));
                context.afterInitializing( init );
                  }
             
             
            private function init():void 
            {
                logger.info( "application ready" );
            }
        }
    }
    

    controller/ConfigureDatabaseCommand.as

    package swc.controller
    {
        import swc.model.StatsModel;
        import swc.services.XMLLoaderService;
         import robotlegs.bender.bundles.mvcs.Command;
         
         public class ConfigureDatabaseCommand extends Command
        {
             [Inject]
                      public var statsModel:StatsModel ;
            [Inject]
            public var service:XMLLoaderService;
             
            override public function execute():void
            {
                trace( "ConfigureDatabaseCommand.execute()");
                service.execute()
            }
         }
    }
    
  5. 5 Posted by zuzu on 24 Feb, 2013 12:44 PM

    zuzu's Avatar

    I haven't tried to run your code but it seems you forgot to inject your commandMap:IEventCommandMap in AppConfig. Could well be the cause.

  6. 6 Posted by vishwas.gagrani on 24 Feb, 2013 01:51 PM

    vishwas.gagrani's Avatar

    I think here is the reason, why i am not getting my event dispatched. Got the code from : https://github.com/darscan/robotlegs-demos-HelloFlex/blob/master/sr...

                             // dispatch the event that is bound to the command
                // send on the next frame (mediator won't be ready this frame)
                setTimeout(function():void {
                    dispatcher.dispatchEvent(new Event(Event.INIT));
                }, 1);
    
  7. 7 Posted by vishwas.gagrani on 24 Feb, 2013 04:25 PM

    vishwas.gagrani's Avatar

    got eventdispatching working finally...
    deleted the project, and used this example directly
    https://github.com/darscan/robotlegs-demos-HelloFlash/tree/master/s...

    Closing the thread . :)
    Thnx
    V.

  8. vishwas.gagrani closed this discussion on 24 Feb, 2013 04:25 PM.

  9. Shaun Smith re-opened this discussion on 24 Feb, 2013 05:10 PM

  10. Support Staff 8 Posted by Shaun Smith on 24 Feb, 2013 05:10 PM

    Shaun Smith's Avatar

    Hi Vishwas,

    The root of your problem was related to how you were configuring things in your example code.

    Configs are only processed towards the end of the context initialization phase, whereas Extensions are supposed to be installed before the context is initialized. You were trying to install the extensions inside of a config, which meant the extensions were being installed too late.

    That is why all the official examples look something like this:

    context.install(Bundles).configure(Configs);
    

    This ensures that the Bundle (and all Extensions in the Bundle) are installed before the Configs are processed.

    In your code you were installing the MVCSBundle inside of the configure() method for a config, which leads to unpredictable results.

    I'm going to take a look and see if I can introduce some code to throw an error if someone does this.

    Hope that helps!

  11. 9 Posted by vishwas.gagrani on 26 Feb, 2013 07:11 PM

    vishwas.gagrani's Avatar

    Ok. thanks for pointing that out. For now, I have just saved a basic template for RL2.0 to keep myself away from these mistakes. :)

    V.

  12. vishwas.gagrani closed this discussion on 26 Feb, 2013 07:11 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