Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Generic and helper module stuff - first version. For use with RobotLegs.

  • Loading branch information...
commit 53bbe7701bf5ad5b0038cac31618e4138f3a3cb9 1 parent 501c2ca
Lindsey Fallow authored
View
52 org/robotlegs/utilities/modular/GenericModule.as
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2009 the original author or authors
+ *
+ * Permission is hereby granted to use, modify, and distribute this file
+ * in accordance with the terms of the license agreement accompanying it.
+ */
+
+/* RECOMMENDED USAGE: Swf modules should extend this class
+
+ They will have a reference to the GenericModuleContext - through context.
+ If you require a more specific reference to the context in your Module then do:
+
+ private var specialContext:SpecialContext;
+*/
+
+
+package org.robotlegs.utilities.modular
+{
+
+ import org.robotlegs.utilities.modular.ModuleEventDispatcher;
+ import org.robotlegs.utilities.modular.IModuleContextView;
+ import org.robotlegs.utilities.modular.GenericModuleContext;
+ import flash.display.MovieClip;
+
+ public class GenericModule extends MovieClip implements IModuleContextView
+ {
+
+ protected var context:GenericModuleContext;
+
+ public function GenericModule():void
+ {
+ // in your extending class do:
+ // context = new SpecialContext(this);
+ // or - if you need to know the specific class of the context
+ // specialContext = new SpecialContext(this);
+ // context = specialContext;
+ }
+
+
+ public function setModuleDispatcher(dispatcher:ModuleEventDispatcher):void
+ {
+ context.setModuleDispatcher(dispatcher);
+ }
+
+ // simply exposes the startup function of the context
+ // don't forget to call setModuleDispatcher before startup...
+ public function startup():void{
+ context.startup();
+ }
+
+ }
+}
View
52 org/robotlegs/utilities/modular/GenericModuleContext.as
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2009 the original author or authors
+ *
+ * Permission is hereby granted to use, modify, and distribute this file
+ * in accordance with the terms of the license agreement accompanying it.
+ */
+
+/* Unless you're just doing a quick demo this class should always be extended */
+
+package org.robotlegs.utilities.modular
+{
+ import org.robotlegs.utilities.modular.ModuleCommandMap;
+ import org.robotlegs.utilities.modular.ModuleEventDispatcher;
+ import org.robotlegs.utilities.modular.GenericModule;
+ import org.robotlegs.mvcs.Context;
+
+ public class GenericModuleContext extends Context
+ {
+ // this var is only a helper to make the diagnosis of errors related to startup() running before setModuleDispatcher more obvious.
+ protected var _isModuleDispatcherSet:Boolean = false;
+
+ public function GenericModuleContext(contextView:GenericModule = null):void
+ {
+ // the module startup can't be run until setModuleDispatcher has happened.
+ // I've put in a boolean to capture this to make diagnostics easier as it's a gotcha.
+
+ super(contextView, false)
+ }
+
+ override public function startup():void
+ {
+ // this has no purpose other than to make it obvious what you've done wrong...
+
+ if(!_isModuleDispatcherSet){
+ trace("DIAGNOSTIC HELPER: You need to set up the module dispatcher before you can run startup. If your app bails now... you'll know why.")
+ }
+
+ mediatorMap.mapView(GenericModule, GenericModuleMediator);
+ super.startup();
+ }
+
+ public function setModuleDispatcher(dispatcher:ModuleEventDispatcher):void
+ {
+ injector.mapValue(ModuleEventDispatcher, dispatcher);
+ injector.mapValue(ModuleCommandMap, new ModuleCommandMap(dispatcher, injector, reflector));
+
+ _isModuleDispatcherSet = true;
+
+ }
+
+ }
+}
View
45 org/robotlegs/utilities/modular/GenericModuleMediator.as
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2009 the original author or authors
+ *
+ * Permission is hereby granted to use, modify, and distribute this file
+ * in accordance with the terms of the license agreement accompanying it.
+ */
+
+/* DO NOT EXTEND THIS CLASS! It's just a template, and can be used for demos but your actual ModuleMediator should
+be based on this class but extend Mediator as this one does. */
+
+package org.robotlegs.utilities.modular
+{
+ import flash.events.Event;
+ import flash.events.MouseEvent;
+
+ import org.robotlegs.utilities.modular.ModuleEventDispatcher;
+ import org.robotlegs.mvcs.Mediator;
+
+ public class GenericModuleMediator extends Mediator
+ {
+ [Inject]
+ public var view:GenericModule;
+
+ [Inject]
+ public var moduleDispatcher:ModuleEventDispatcher;
+
+ override public function onRegister():void
+ {
+ eventMap.mapListener(view, MouseEvent.CLICK, onViewClick);
+ eventMap.mapListener(moduleDispatcher, "SomeModuleEvent", onSomeModuleEvent );
+ }
+
+ protected function onViewClick(e:MouseEvent):void
+ {
+ moduleDispatcher.dispatchEvent(new Event("SomeModuleEvent"));
+ }
+
+ protected function onSomeModuleEvent(e:Event):void
+ {
+ view.alpha *= 0.8;
+ (view.alpha > 0.1) || (view.alpha = 0.9);
+ }
+
+ }
+}
View
15 org/robotlegs/utilities/modular/IModuleContextView.as
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2009 the original author or authors
+ *
+ * Permission is hereby granted to use, modify, and distribute this file
+ * in accordance with the terms of the license agreement accompanying it.
+ */
+
+package org.robotlegs.utilities.modular
+{
+
+ public interface IModuleContextView
+ {
+ function setModuleDispatcher(dispatcher:ModuleEventDispatcher):void;
+ }
+}
View
23 org/robotlegs/utilities/modular/ModuleCommandMap.as
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2009 the original author or authors
+ *
+ * Permission is hereby granted to use, modify, and distribute this file
+ * in accordance with the terms of the license agreement accompanying it.
+ */
+
+package org.robotlegs.utilities.modular
+{
+ import flash.events.IEventDispatcher;
+
+ import org.robotlegs.base.CommandMap;
+ import org.robotlegs.core.IInjector;
+ import org.robotlegs.core.IReflector;
+
+ public class ModuleCommandMap extends CommandMap
+ {
+ public function ModuleCommandMap(eventDispatcher:IEventDispatcher, injector:IInjector, reflector:IReflector)
+ {
+ super(eventDispatcher, injector, reflector);
+ }
+ }
+}
View
20 org/robotlegs/utilities/modular/ModuleEventDispatcher.as
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2009 the original author or authors
+ *
+ * Permission is hereby granted to use, modify, and distribute this file
+ * in accordance with the terms of the license agreement accompanying it.
+ */
+
+package org.robotlegs.utilities.modular
+{
+ import flash.events.EventDispatcher;
+ import flash.events.IEventDispatcher;
+
+ public class ModuleEventDispatcher extends EventDispatcher
+ {
+ public function ModuleEventDispatcher(target:IEventDispatcher = null)
+ {
+ super(target);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.