Over 10 years of community discussion and knowledge are maintained here as a read-only archive.
override public function onRegister():void { // Create and register Mediators // components that were instantiated by the mxml application facade.registerMediator( new SplashScreenMediator( app.splashScreen ) ); facade.registerMediator( new MainScreenMediator( app.mainScreen ) ); }
http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/spark/components/ViewNavigator.html
navigator.pushView(AddEditView)
spark.components.ViewNavigator.pushView(factory:Class, data:Object=null, transition:ViewTransition=null):voidPushes a new view to the top of the navigation stack.Parameters:factory The class used to create the viewdata The data object to pass to the viewtransition The view transition to play
<?xml version="1.0" encoding="utf-8"?><s:MobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" firstView="views.ViewNavigationHome"> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> </s:MobileApplication>
<?xml version="1.0" encoding="utf-8"?><s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="Home" initialize="init()"> <fx:Script> <![CDATA[ import mx.collections.ArrayCollection; [Bindable] private var people:ArrayCollection; private function init():void { people = new ArrayCollection(); var somebody:Object = new Object(); // TODO } private function handleClick():void { navigator.pushView( PeopleDetails, peopleList.selectedItem ); } ]]></fx:Script> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <s:List id="peopleList" click="handleClick()" dataProvider="{people}" labelField="name" width="100%" height="100%"/> </s:View>
<?xml version="1.0" encoding="utf-8"?><s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="Person Details"> <fx:Script> <![CDATA[private function gotoHome():void{ navigator.popToFirstView();} ]]> </fx:Script> <s:layout> <s:VerticalLayout paddingBottom="5" paddingLeft="5" paddingRight="5" paddingTop="5"/> </s:layout> <s:navigationContent> <s:Button label="Home" click="gotoHome()"/> </s:navigationContent> <s:Form width="100%" height="100%"> <s:FormItem label="Name:" width="100%"> <s:Label text="{data.name}"/> </s:FormItem> </s:Form> </s:View>
I'm registering a navigatorMediator as MainMediator & it holds a reference to the Navigator and a <vector> with all views. When the user navigates, the navigatorMediator is called, registers a dependent mediator and so on.This is somewhat counterintuitive and I'm wondering if there's a better solution.
It feels laborious to remove / instantiate new Mediators every time a view is pushed
<?xml version="1.0" encoding="utf-8"?><s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" firstView="views.TemplateHomeView" applicationDPI="240" initialize="onInitalization(event)"> <fx:Script> <![CDATA[ import com.digitaleavantgarde.ApplicationFacade; import mx.events.FlexEvent; private var facade:ApplicationFacade = ApplicationFacade.getInstance(); protected function onInitalization(event:FlexEvent):void { // Booting Framework facade.startup(this.navigator); } ]]> </fx:Script></s:ViewNavigatorApplication>
public function ApplicationMediator(viewNavigator:Object=null) { super( NAME, viewNavigator ); viewNavigator.addEventListener(ElementExistenceEvent.ELEMENT_ADD, function(evt:ElementExistenceEvent):void { var activeView:View = evt.element as View; trace(activeView.title); // traces the title }); }
public function ViewNavigatorMediator(viewComponent:MyHeroApplication) { super( NAME, viewComponent ); } private function get viewNavigator():MyHeroApplication { return viewComponent as MyHeroApplication } override public function onRegister():void { viewNavigator.addEventListener(ElementExistenceEvent.ELEMENT_ADD, onElementAdd) } private function onElementAdd(event:ElementExistenceEvent):void { var activeView:View = event.element as View; trace(activeView.title); // traces the title // now, let a command figure out which mediator to register... sendNotification( MyAppConstants.MEDIATE_VIEW, activeView ); }
protected function onInitalization(event:FlexEvent):void { this.id = NAME; }
package com.digitaleavantgarde.view{ import com.digitaleavantgarde.ApplicationFacade; import com.digitaleavantgarde.model.LocalStorageProxy; import com.digitaleavantgarde.utils.lang.ResourceLanguage; import flash.net.registerClassAlias; import org.puremvc.as3.interfaces.IMediator; import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.mediator.Mediator; import spark.components.View; import spark.components.ViewNavigator; import spark.events.ElementExistenceEvent; import views.AnotherView; import views.HomeView; public class ApplicationMediator extends Mediator implements IMediator { public static const NAME:String = 'ApplicationMediator'; private var viewNavigator:ViewNavigator; private var activeView:View; public function ApplicationMediator(viewNavigator:ViewNavigator) { this.viewNavigator = viewNavigator; super( NAME, viewNavigator ); } override public function onRegister():void { viewNavigator.addEventListener(ElementExistenceEvent.ELEMENT_ADD, switchView); } override public function listNotificationInterests():Array { return [ ApplicationFacade.LOG, LocalStorageProxy.LANGUAGE ]; } override public function handleNotification(notification:INotification):void { var name:String = notification.getName(); switch (name) { case ApplicationFacade.LOG: trace(notification.getBody()); break; case LocalStorageProxy.LANGUAGE: ResourceLanguage.getInstance().setLang(notification.getBody() as String); if(activeView!=null) { var viewToUpdate:ILanguageMediator = this.facade.retrieveMediator(activeView.id) as ILanguageMediator; viewToUpdate.setLang(); } break; } } private function switchView(evt:ElementExistenceEvent):void { if(activeView!=null) { this.facade.removeMediator(activeView.id + 'Mediator'); } activeView = evt.element as View; switch(activeView.id) { case(HomeView.NAME): this.facade.registerMediator(new HomeViewMediator(activeView)); break; case (AnotherView.NAME): this.facade.registerMediator(new AnotherViewMediator(activeView)); break; } } }}