I've tried tearing everything down and started over, in an attempt to use commands for most of the logic, but I must admit I am still rather confused and there are elements of my implementation that seem rather "unhandy".
My removing of the old page and building of a new page is now a macro command (invoked ApplicationFacade.CHANGE_PAGE) that looks like this:
override protected function initializeMacroCommand():void {
// remove previous page view and mediator
addSubCommand(RemovePageCommand);
// resolve files and stuff for the next view
addSubCommand(ResolvePageCommand);
// create new page view and mediator
addSubCommand(CreatePageCommand);
// start loading data for new page
addSubCommand(LoadPageDataCommand);
}
The RemovePageCommand looks like this:
override public function execute(note:INotification):void {
var currentDisplay:Display = MainDisplayMediator(facade.retrieveMediator( MainDisplayMediator.NAME )).getCurrentPage();
if ( currentDisplay != null ) {
currentDisplay.transitionOut();
}
}
As you can see this command fetches the current page from the MainDisplayMediator, I don't know where else to store the page being displayed? (the button that invokes the CHANGE_PAGE is not within the MainDisplay it is a global menu, and it knows nothing of the mainDisplay)
Then there's the ResolvePageCommand
override public function execute(note:INotification):void {
// get the page name
var pVo:PageChangeVO = note.getBody() as PageChangeVO;
var siteTree:SiteTreeVO = SiteTreeProxy(facade.retrieveProxy( SiteTreeProxy.NAME )).getSiteTree();
var file:String = siteTree.getFileByName(pVo.nextPage);
// modify the notification body
PageChangeVO(note.getBody()).nextPageDataFile = file;
}
This basically just does a lookup in a site tree xml to find the proper file to load for the next view. As you can see it modifies the body of the notification before the next command recieves it. Is that good practice?
On to the CreatePageCommand:
override public function execute(note:INotification):void {
var pVo:PageChangeVO = note.getBody() as PageChangeVO;
var pageName = pVo.nextPage;
var mainDisplayMediator:MainDisplayMediator = facade.retrieveMediator( MainDisplayMediator.NAME ) as MainDisplayMediator;
var mainDisplay:MainDisplay = mainDisplayMediator.mainDisplay;
var display:Display;
if ( pageName == MainDisplay.CONTACT_VIEW ) {
display = new ContactDisplay();
facade.registerMediator( new ContactDisplayMediator( display ) );
}
else if ( pageName == MainDisplay.FIND_DEALER_VIEW ) {
display = new FindDealerDisplay();
facade.registerMediator( new FindDealerDisplayMediator( display ) );
}
mainDisplay.setView( display );
mainDisplayMediator.setCurrentPage( display );
}
This is where it gets strange. As you can see I have methods for retrieving both the mainDisplay container and the MainDisplayMediator, and this seems wrong. But I couldn't see how else to make a command able to add an item to the display list?
Finally I load the data for the newly created page, all it does is tell a PageProxy to load the data, and when that is done the Proxy isuues a PAGE_DATA_LOADED notification which all "PageMediators" listens for. Here it get eerie again, to determine if the notification is actually relevant for the Mediator it compares the currentpage of the MainDisplayMediator to it's own view component:
override public function handleNotification(note:INotification):void {
switch (note.getName()) {
case ApplicationFacade.PAGE_DATA_LOADED:
if ( MainDisplayMediator(facade.retrieveMediator( MainDisplayMediator.NAME)).getCurrentPage() == findDealerDisplay ) {
findDealerDisplay.setData( note.getBody() as XML );
findDealerDisplay.transitionIn();
}
break;
default:
break;
}
}
I know this is a lot of code, but since its run sequentially through a macrocommand it should be straight forward. The only issues are:
Can I store information on my MainDisplayMediator that the commands need to determine what to remove?
Can I expose the mainDisplay display object to a command to make it able to set a display object as a child?
Can I modify the body of a notification as it goes through a macrocommands subcommands?
Any and all remarks/critisism are more than welcome. I am prepared to take it all apart and start over if neccesary, I want to get this right
Regards