tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/869-dispatching-event-from-appconfig-to-start-database-configurationRobotlegs: Discussion 2018-10-18T16:35:46Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/252464942013-02-23T22:52:25Z2013-02-23T22:52:26ZDispatching event from AppConfig to start database configuration<div><p>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:</p>
<pre>
<code>[Inject]
public var dispatcher:IEventDispatcher;</code>
</pre>
<p>and use it like this:</p>
<pre>
<code>dispatcher.dispatchEvent(new DatabaseEvent( DatabaseEvent.CONFIGURE));</code>
</pre>
<p>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:</p>
<pre>
<code>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));
}
}</code>
</pre>
<p>Hope that helps<br>
~Christina</p></div>zuzutag:robotlegs.tenderapp.com,2009-10-18:Comment/252464942013-02-24T05:31:21Z2013-02-24T06:42:37ZDispatching event from AppConfig to start database configuration<div><p>thnx Christina.</p>
<p>Btw.. I am getting this error in the above :<br>
Injector is missing a mapping to handle injection into target
"[class AppConfig]" of type "swc::AppConfig".</p>
<p>V.</p></div>vishwas.gagranitag:robotlegs.tenderapp.com,2009-10-18:Comment/252464942013-02-24T11:26:54Z2013-02-24T11:26:54ZDispatching event from AppConfig to start database configuration<div><p>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.</p>
<p>A minimal setup may look like this:<br></p>
<pre>
<code>_context = new Context()
.install(MVCSBundle)
.configure(MyAppConfig, new ContextView(this));</code>
</pre>
<p>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.</p></div>zuzutag:robotlegs.tenderapp.com,2009-10-18:Comment/252464942013-02-24T12:10:23Z2013-02-24T12:18:18ZDispatching event from AppConfig to start database configuration<div><p>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</p>
<p>Main.as<br></p>
<pre>
<code>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;<br>
<br>
</code>
</pre>
<pre>
<code><code>[Frame(factoryClass = &quot;app.Preloader&quot;)]
public class Main extends Sprite
{
protected var _context:IContext;
public function Main()
{
trace(&quot;Main&quot;);
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 ));
}
}</code></code>
</pre>
<pre>
<br>
}
</pre>
<p>AppConfig.as :</p>
<pre>
<code>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" );
}
}
}</code>
</pre>
<p>controller/ConfigureDatabaseCommand.as</p>
<pre>
<code>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()
}
}
}</code>
</pre></div>vishwas.gagranitag:robotlegs.tenderapp.com,2009-10-18:Comment/252464942013-02-24T12:44:57Z2013-02-24T12:44:59ZDispatching event from AppConfig to start database configuration<div><p>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.</p></div>zuzutag:robotlegs.tenderapp.com,2009-10-18:Comment/252464942013-02-24T13:51:43Z2013-02-24T13:51:43ZDispatching event from AppConfig to start database configuration<div><p>I think here is the reason, why i am not getting my event
dispatched. Got the code from : <a href=
"https://github.com/darscan/robotlegs-demos-HelloFlex/blob/master/src/robotlegs/bender/demos/hello/config/AppConfig.as">
https://github.com/darscan/robotlegs-demos-HelloFlex/blob/master/sr...</a></p>
<pre>
<code> // 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);</code>
</pre></div>vishwas.gagranitag:robotlegs.tenderapp.com,2009-10-18:Comment/252464942013-02-24T16:25:11Z2013-02-24T16:25:11ZDispatching event from AppConfig to start database configuration<div><p>got eventdispatching working finally...<br>
deleted the project, and used this example directly<br>
<a href=
"https://github.com/darscan/robotlegs-demos-HelloFlash/tree/master/src/robotlegs/bender/demos/hello">
https://github.com/darscan/robotlegs-demos-HelloFlash/tree/master/s...</a></p>
<p>Closing the thread . :)<br>
Thnx<br>
V.</p></div>vishwas.gagranitag:robotlegs.tenderapp.com,2009-10-18:Comment/252464942013-02-24T17:10:57Z2013-02-24T17:10:57ZDispatching event from AppConfig to start database configuration<div><p>Hi Vishwas,</p>
<p>The root of your problem was related to how you were configuring
things in your example code.</p>
<p>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.</p>
<p>That is why all the official examples look something like
this:</p>
<pre>
<code>context.install(Bundles).configure(Configs);</code>
</pre>
<p>This ensures that the Bundle (and all Extensions in the Bundle)
are installed before the Configs are processed.</p>
<p>In your code you were installing the MVCSBundle <em>inside</em>
of the configure() method for a config, which leads to
unpredictable results.</p>
<p>I'm going to take a look and see if I can introduce some code to
throw an error if someone does this.</p>
<p>Hope that helps!</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/252464942013-02-26T19:11:10Z2013-02-26T19:11:10ZDispatching event from AppConfig to start database configuration<div><p>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.
:)</p>
<p>V.</p></div>vishwas.gagrani