Over 10 years of community discussion and knowledge are maintained here as a read-only archive.
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:view = "sampleApplication.view.component.*" creationComplete="facade.startup(this)"> <mx:Script> <![CDATA[ private var facade:ApplicationFacade = ApplicationFacade.getInstance(); ]]> </mx:Script> <mx:VBox horizontalAlign="center" verticalAlign="middle"> <view:HelloPanel id="hello" width = "300" height="300" horizontalAlign="center" verticalAlign="middle"/> </mx:VBox></mx:Application>
package{ import org.puremvc.as3.interfaces.IFacade; import org.puremvc.as3.patterns.facade.Facade; import sampleApplication.controller.StartupCommand; public class ApplicationFacade extends Facade implements IFacade { public static const STARTUP:String = "startup"; public static const HELLO_OK_CLICKED:String = "helloOkClicked"; public static function getInstance():ApplicationFacade { if (instance == null) { instance = new ApplicationFacade(); } return instance as ApplicationFacade; } public function startup(app:Object):void { sendNotification(STARTUP, app); } override protected function initializeController():void { super.initializeController(); registerCommand(STARTUP, StartupCommand); } }}
<?xml version="1.0" encoding="utf-8"?><mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" title="test"> <mx:Button id="btnOk" x="44.5" y="40" label="ok" width="78"/> <mx:TextInput id="txName" x="7" y="10"/> </mx:Panel>
package sampleApplication.view{ import flash.events.Event; import flash.events.MouseEvent; import org.puremvc.as3.interfaces.IMediator; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.mediator.Mediator; import sampleApplication.controller.MakeHelloCommand; import sampleApplication.view.component.HelloPanel; public class HelloPanelMediator extends Mediator implements IMediator { public static const NAME:String="HelloPanelMediator"; public function HelloPanelMediator(viewComponent:Object=null) { super(NAME, viewComponent); } override public function onRegister():void { helloPanel.btnOk.addEventListener(MouseEvent.CLICK, onOkClick); } private function onOkClick(evt:Event):void { sendNotification(MakeHelloCommand.CMD_MAKE_HELLO, helloPanel.txName.text); } override public function listNotificationInterests():Array { return [MakeHelloCommand.CMD_MAKE_HELLO]; } override public function handleNotification(notification:INotification):void { switch (notification.getName()) { case MakeHelloCommand.CMD_MAKE_HELLO: break; } } private function get helloPanel():HelloPanel { return viewComponent as HelloPanel; } }}
package sampleApplication.controller{ import org.puremvc.as3.interfaces.ICommand; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.command.SimpleCommand; import sampleApplication.model.HelloProxy; import sampleApplication.view.HelloPanelMediator; public class StartupCommand extends SimpleCommand implements ICommand { override public function execute(notification:INotification):void { facade.registerProxy(new HelloProxy()); var app:SampleProject = notification.getBody() as SampleProject; facade.registerMediator(new HelloPanelMediator(app.hello)); } }}
package sampleApplication.controller{ import org.puremvc.as3.interfaces.ICommand; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.command.SimpleCommand; public class MakeHelloCommand extends SimpleCommand implements ICommand { public static const NAME:String = "MakeHello"; public static const CMD_MAKE_HELLO:String = "MakeHello.MakeHello"; public function MakeHelloCommand() { super(); } override public function execute(notification:INotification):void { } }}
package sampleApplication.model{ import org.puremvc.as3.interfaces.IProxy; import org.puremvc.as3.patterns.proxy.Proxy; public class HelloProxy extends Proxy implements IProxy { public static const NAME:String = "HelloProxy"; public function HelloProxy(data:Object = null) { super(NAME, data); } }}
private function onOkClick(evt:Event):void { sendNotification(MakeHelloCommand.CMD_MAKE_HELLO, helloPanel.txName.text); }
private function onOkClick(evt:Event):void { sendNotification(ApplicationFacade.HELLO_OK_CLICKED, helloPanel.txName.text); }
package sampleApplication.view{... import sampleApplication.controller.MakeHelloCommand;... override protected function initializeController():void { registerCommand(STARTUP, StartupCommand); registerCommand(HELLO_OK_CLICKED, MakeHelloCommand); }...
import mx.controls.Alert; public class MakeHelloCommand extends SimpleCommand implements ICommand { override public function execute(notification:INotification):void { mx.controls.Alert.show("Hello", "Hi there"); } }
package{ import controller.HelloButtonClickCommand; import controller.LoginHardcoddedCommand; import controller.SquareNumberCommand; import controller.StartupCommand; import org.puremvc.as3.interfaces.IFacade; import org.puremvc.as3.patterns.facade.Facade; /** * Facade klasa se uvek pravi, ona omogucava mediatorima, proxijima i komandama * da komuniciraju. * U njoj se nalaze konstante svih notifikacija. * Singleton metod (getInstance) * Registracija svih komandi (?) * startup metod koji startuje aplikaciju i poziva se sa main.mxml fajla * @author vvucetic * */ public class ApplicationFacade extends Facade implements IFacade { // list od all notifications public static const STARTUP:String = "startup"; public static const HELLO_BUTTON_CLICKED:String = "helloButtonClicked"; public static const SQUARE_BUTTON_CLICKED:String = "squareButtonClicked"; public static const LOGIN_HARDCODE:String = "loginHardcode"; public static function getInstance():ApplicationFacade { if (instance == null) { instance = new ApplicationFacade(); } return instance as ApplicationFacade; } public function startup(app:Object):void { sendNotification(STARTUP, app); } override protected function initializeController():void { super.initializeController(); // registers commands registerCommand(STARTUP, StartupCommand); registerCommand(HELLO_BUTTON_CLICKED, HelloButtonClickCommand); registerCommand(SQUARE_BUTTON_CLICKED, SquareNumberCommand); registerCommand(LOGIN_HARDCODE, LoginHardcoddedCommand); } }}
package controller{ import org.puremvc.as3.interfaces.ICommand; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.command.SimpleCommand; import view.HelloWindowComponentMediator; /** * Uvek postoji * Registruje mediatore * @author vvucetic * */ public class StartupCommand extends SimpleCommand implements ICommand { override public function execute(notification:INotification):void { var app:HelloVlada = notification.getBody() as HelloVlada; facade.registerMediator(new HelloWindowComponentMediator(app.HelloPanel)); } }}
package controller{ import mx.controls.Alert; import org.puremvc.as3.interfaces.ICommand; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.command.SimpleCommand; public class HelloButtonClickCommand extends SimpleCommand implements ICommand { override public function execute(notification:INotification):void { Alert.show("hello, " + notification.getBody() as String); } }}
package controller{ import model.vo.Login; import mx.controls.Alert; import org.puremvc.as3.interfaces.ICommand; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.command.SimpleCommand; public class LoginHardcoddedCommand extends SimpleCommand implements ICommand { override public function execute(notification:INotification):void { var userData:Login; userData = notification.getBody() as Login; if (userData.username == "vlada" && userData.password == "vlada") { Alert.show("welcome vlada"); } else { Alert.show("wrong password"); } } }}
<?xml version="1.0" encoding="utf-8"?><mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="400" title="test"> <!-- Simple component to test pureMVC framework--> <mx:TextInput id="txName" text="vlada" x="10" y="10" width="160"/> <mx:Button id="btnHello" label="hello" x="178" y="10" width="192" height="22"/> <mx:TextInput id="txNumberToSquare" x="10" y="40" width="76"/> <mx:Button id="btnSquare" x="178" y="40" label="square" width="192"/> <mx:Button id="btnLogin" x="153" y="166" label="login"/> <mx:TextInput id="txUsername" x="80" y="110" width="126"/> <mx:TextInput id="txPassword" x="80" y="136" width="126"/> <mx:Label x="10" y="112" text="Username"/> <mx:Label x="10" y="138" text="Password"/></mx:Panel>
package view{ import flash.events.Event; import flash.events.MouseEvent; import model.vo.Login; import org.puremvc.as3.interfaces.IMediator; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.mediator.Mediator; import view.components.HelloWindowComponent; /** * Mediator postoji za svaku komponentu i jedini osluskuje sta se desava * na komponenti i na osnovu toga obavestava ostatak sistema. * onRegister - registruju se svi dogadjaji * listNotificationInterests - sve notifikacije koje ovaj mediator zanimaju * handleNotification - handler za gore navedene notifikacije * @author vvucetic * */ public class HelloWindowComponentMediator extends Mediator implements IMediator { public static const NAME:String = "HelloWindowComponentMediator"; public function HelloWindowComponentMediator(viewComponent:Object=null) { super(NAME, viewComponent); } private function get helloWindowComponent():HelloWindowComponent { return viewComponent as HelloWindowComponent; } override public function onRegister():void { helloWindowComponent.btnHello.addEventListener(MouseEvent.CLICK, onHelloButtonClick); helloWindowComponent.btnSquare.addEventListener(MouseEvent.CLICK, onSquareButtonClick); helloWindowComponent.btnLogin.addEventListener(MouseEvent.CLICK, onLoginButtonClick); } private function onLoginButtonClick(evt:Event):void { var userLoginData:Login = new Login(); userLoginData.username = helloWindowComponent.txUsername.text; userLoginData.password = helloWindowComponent.txPassword.text; sendNotification(ApplicationFacade.LOGIN_HARDCODE, userLoginData); } private function onHelloButtonClick(evt:Event):void { sendNotification(ApplicationFacade.HELLO_BUTTON_CLICKED, helloWindowComponent.txName.text); } private function onSquareButtonClick(evt:Event):void { sendNotification(ApplicationFacade.SQUARE_BUTTON_CLICKED, helloWindowComponent.txNumberToSquare.text); } override public function listNotificationInterests():Array { return [ApplicationFacade.HELLO_BUTTON_CLICKED, ApplicationFacade.SQUARE_BUTTON_CLICKED, ApplicationFacade.LOGIN_HARDCODE]; } override public function handleNotification(notification:INotification):void { switch (notification.getName()) { case ApplicationFacade.HELLO_BUTTON_CLICKED: break; case ApplicationFacade.SQUARE_BUTTON_CLICKED: break; case ApplicationFacade.LOGIN_HARDCODE: break; } } }}
Looks like you're on your way here. This is all just fine. Alert boxes from commands are OK. When you need a more sophisticated dialog to gather info from the user, you may want to make it a component that gets its own mediator (for its short lifetime). Here's some good info on popup handing from Simon Bailey:http://www.nutrixinteractive.com/blog/?p=329-=Cliff>