So I followed he helpful information (for ViewStack at least):
http://forums.puremvc.org/index.php?topic=280.0However I still have the same problem.
ApplicationMediator public function ApplicationMediator(viewComponent:Object=null)
{
super(NAME, viewComponent);
view.addEventListener(AppMain.EVT_SHOW_FOO, onShowFoo);
view.addEventListener(AppMain.EVT_SHOW_BAR, onShowBar);
checkForMediator(view.currentViewSelector, view.activeView);
}
protected function onShowFoo(e:Event):void {
view.currentViewSelector = AppMain.VIEW_FOO;
checkForMediator(AppMain.VIEW_FOO, view.activeView);
sendNotification(ApplicationFacade.FOO_MODE);
}
protected function onShowBar(e:Event):void {
view.currentViewSelector = AppMain.VIEW_BAR;
checkForMediator(AppMain.VIEW_BAR, view.activeView);
sendNotification(ApplicationFacade.BAR_MODE);
}
protected function checkForMediator( childSelector:int, child:Object ):void {
switch (childSelector) {
case AppMain.VIEW_FOO:
if (facade.retrieveMediator(FooViewMediator.NAME) == null) {
facade.registerMediator(new FooViewMediator(child));
}
break;
case AppMain.VIEW_BAR:
if (facade.retrieveMediator(BarViewMediator.NAME) == null) {
facade.registerMediator(new BarViewMediator(child));
}
break;
}
}
AppMain.mxml <mx:Script>
<![CDATA[
import mx.events.ItemClickEvent;
import mx.rpc.events.FaultEvent;
public static const EVT_SHOW_FOO:String = "showFoo";
public static const EVT_SHOW_BAR:String = "showBar";
public static const VIEW_FOO:int = 0;
public static const VIEW_BAR:int = 1;
[Bindable] public var currentViewSelector:int = VIEW_FOO;
public var activeView:Object = viewEvents;
private var facade:ApplicationFacade = ApplicationFacade.getInstance();
private function init():void {
activeView = viewStack.selectedChild;
facade.startup(this);
}
private function submit(action:String):void {
dispatchEvent(new Event(action, true));
}
]]>
</mx:Script>
<mx:Binding source="viewStack.selectedChild" destination="activeView" />
<mx:ApplicationControlBar id="appControlBar" width="65%" dock="true">
<mx:Button id="buttonFoo" label="Foo" click="submit(EVT_SHOW_FOO)" />
<mx:Button id="buttonBar" label="Bar" click="submit(EVT_SHOW_BAR)" />
</mx:ApplicationControlBar>
<mx:ViewStack id="viewStack" width="100%" height="75%"
selectedIndex="{currentViewSelector}">
<components:FooView id="viewFoo" width="100%" height="100%" />
<components:BarView id="viewBar" width="100%" height="100%" />
</mx:ViewStack>
So when the app first starts up you see the "FooView". When I click on the "Bar" button I get the NULL reference error in my BarViewMediator when I try to set the dataProvider on my "listBarObjects":
BarViewMediator.as public function BarViewMediator(viewComponent:Object=null) {
super(NAME, viewComponent);
var data:ArrayCollection = new ArrayCollection();
for (var i:int = 0; i < 5; i++) {
var q:BarVO = new BarVO ();
q.name = "Bar " + (i+1);
q.msg = "something random here - " + i;
data.addItem(q);
}
view.listBarObjects.dataProvider = data;
}
Did I miss something about how to handle this deferred instantiation? When I debug I can see that the "checkForMediator" method gets called when I click the "Bar" button and I see it creates a new "BarViewMediator" and passes it the "BarView" object, however the "listBarObjects" is null.