tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/1425-module-without-viewRobotlegs: Discussion 2012-12-11T11:30:03Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/221870382012-12-06T10:24:36Z2012-12-06T10:24:36ZModule without view<div><p>Hello,<br>
we are creating a project with modules.</p>
<p>Everything works fine.<br>
But now we want to create a module without any view .</p>
<p>This module would be a service responsible for loading language
files and notifying other modules when the data is loaded.<br>
Is this possible?</p></div>Barbarossotag:robotlegs.tenderapp.com,2009-10-18:Comment/221870382012-12-07T08:51:29Z2012-12-07T08:51:29ZModule without view<div><p>Hi Barbarosso,</p>
<p>I suppose you’re using robotlegs v.1 with the modular
utility, right?</p>
<p>In my example below, ViewLessModule is a Module class and
SomeService is a class extending ModuleMediator or ModuleActor,
which have access to the moduleEventDispatcher. If you don’t
want to extend these classes, you have to inject an
IModuleEventDispatcher, as I did in the second part of my
example.</p>
<h2>1.</h2>
<p>ShellContext:<br>
viewMap.mapType(IModule);//org.robotlegs.utilities.modular.core.IModule;</p>
<p>ShellView:<br>
[-----------] code for loading modules //don’t forget
ApplicationDomain!!</p>
<p>Add the module to the shell’s stage, even if it’s
not a visual component!<br>
addElement(moduleLoader as IVisualElement);</p>
<p>ViewLessContext ModuleContext</p>
<pre>
<code> public class ViewLessContext extends ModuleContext
{
private var someService:SomeService;
public function ViewLessContext(contextView:DisplayObjectContainer = null, autoStartup:Boolean = true, parentInjector:IInjector = null)
{
trace("ViewLessContext.ViewLessContext()");
super(contextView, autoStartup, parentInjector);
}
override public function startup():void
{
trace("ViewLessContext.startup()");
injector.mapSingleton(SomeService);
someService = injector.getInstance(SomeService);
super.startup();
}
}</code>
</pre>
<p>ViewLessModule as Module (spark.modules.Module)</p>
<pre>
<code> public class ViewLessModule extends Module implements IModule
{
import org.robotlegs.core.IInjector;
import org.robotlegs.utilities.modular.core.IModule;
import org.robotlegs.utilities.modular.core.IModuleContext;
private var context:IModuleContext;
public function ViewLessModule()
{
trace("ViewLessModule.ViewLessModule()");
}
[Inject]
public function set parentInjector(value:IInjector):void
{
trace("ViewLessModule.parentInjector(value)");
context = new ViewLessContext(this, true, value);
}
public function dispose():void
{
context.dispose();
context = null;
}
}</code>
</pre>
<p>SomeService as a Mediator</p>
<pre>
<code> public class SomeService extends ModuleMediator
{
public function SomeService()
{
trace("SomeOtherClass.SomeOtherClass()");
}
[PostConstruct]
public function addListeners():void
{
trace("SomeService.addListeners()");
addModuleListener(SomeEvent.DATA_REQUESTED, onDataRequested);
}
private function loadSomeData():void
{
trace("SomeService.loadSomeData()");
dispatchToModules(new SomeEvent(SomeEvent.DATA_LOADED, "some data from SomeService"));
}
private function onDataRequested(event:SomeEvent):void
{
trace("SomeService.onDataRequested() " + event.message);
loadSomeData();
}
}</code>
</pre>
<p>SomeService could extend ModuleActor as well.</p>
<h2>2.</h2>
<p>Alternatively, you can do this:</p>
<pre>
<code> public class ViewLessModule extends Module implements IModule
{
import org.robotlegs.core.IInjector;
import org.robotlegs.utilities.modular.core.IModule;
import org.robotlegs.utilities.modular.core.IModuleContext;
private var context:IModuleContext;
[Inject]
public var moduleDispatcher:IModuleEventDispatcher;
public function ViewLessModule()
{
trace("ViewLessModule.ViewLessModule()");
}
[PostConstruct]
public function initService():void
{
trace("ViewLessModule.doSomething()");
moduleDispatcher.addEventListener(SomeEvent.DATA_REQUESTED, onDataRequested);
}
[Inject]
public function set parentInjector(value:IInjector):void
{
trace("ViewLessModule.parentInjector(value)");
context = new ViewLessContext(this, true, value);
}
private function onDataRequested(event:SomeEvent):void
{
trace("ViewLessModule.onDataRequested() " + event.message);
moduleDispatcher.dispatchEvent(new SomeEvent(SomeEvent.DATA_LOADED, " from SomeService"));
}
public function dispose():void
{
context.dispose();
context = null;
}
}</code>
</pre>
<p>I’d prefer the first option, though.</p>
<p>Does this help?</p>
<p>Ondina</p></div>Ondina D.F.