Thanks for pointing that out.
I've adjusted my ApplicationMediator. I had a discussion in the german flashforum. Someone gave me the interesting opinion, that the Mediator are upstreamed Controllers (while I was thinking there are downstreamed views ...).
Thus, most of the time I have to main Heroes in my Apps: The ApplicationMediator & the ApplicationProxy: The ApplicationProxy hosts all neccessary data & Proxies that are coupled to a concrete source (e.g. XMLProxy, DatabaseProxy ...) report to him & the VO. Thus, other proxies are more or less plugins to the Main ApplicationProxy.
On the other hand, the ApplicationMediator hosts a reference to the BaseView & handles some stuff like listening for Events that all Mediators care about.
Thus, I'm registering Mediators directly from the ApplicationMediator (and am using Commands rarely). Guess that's bad practice, huh?
Here's my final version.
E.g. the ApplicationFacade.LOG notification is logged always when something bad happens (Error Events). If I want to handle it via AlertBoxes or redirect it to the Javascript console or whatever, I can do it here.
The ResourceLanguage Change (Basically it makes the IResourceManager available from none flex-based as3 files & manages access to language classes) should result in update the language in all Mediators - (setLang() is defined by the interface). Thus, an ApplicationMediator comes in handy for me.
Another Problem on the ViewNavigator: The element property of the ElementExistenceEvent returns a View - thus I cannot access the MyMediator.NAME const, as best practise would recommend without casting every single Mediator.
That's why I set the id == MyMediator.NAME on initalize & use it as synonym in the AppMediator ...
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;
}
}
}
}
This works great for me, but I'm bit afraid of programming cross the purpose of the framework