Module without view

Barbarosso's Avatar

Barbarosso

06 Dec, 2012 10:24 AM

Hello,
we are creating a project with modules.

Everything works fine.
But now we want to create a module without any view .

This module would be a service responsible for loading language files and notifying other modules when the data is loaded.
Is this possible?

  1. Support Staff 1 Posted by Ondina D.F. on 07 Dec, 2012 08:51 AM

    Ondina D.F.'s Avatar

    Hi Barbarosso,

    I suppose you’re using robotlegs v.1 with the modular utility, right?

    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.

    1.

    ShellContext:
    viewMap.mapType(IModule);//org.robotlegs.utilities.modular.core.IModule;

    ShellView:
    [-----------] code for loading modules //don’t forget ApplicationDomain!!

    Add the module to the shell’s stage, even if it’s not a visual component!
    addElement(moduleLoader as IVisualElement);

    ViewLessContext ModuleContext

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

    ViewLessModule as Module (spark.modules.Module)

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

    SomeService as a Mediator

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

    SomeService could extend ModuleActor as well.

    2.

    Alternatively, you can do this:

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

    I’d prefer the first option, though.

    Does this help?

    Ondina

  2. Ondina D.F. closed this discussion on 11 Dec, 2012 11:30 AM.

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