Over 10 years of community discussion and knowledge are maintained here as a read-only archive.
I need to fire an event on creation of Main to register it's mediator, but now what of my Main component's components? Although the creationComplete event fired, and now my mediator is registered all of the sub-components still don't exist so in the constructor of my MainMediator, where I'd like to register event handlers (for instance, to handle an click event from a button in my component) viewComponent.myButton is *still* null..
private function get mainView():MainView{ return viewComponent as MainView;}
You shouldn't set event listeners in your Mediator constructor, but instead, in onRegister. Also it's is necessary in MultiCore, but a best practice for both versions to hold off any facade interaction you'd be tempted to do in the constructor until onRegister as well.
<mx:Canvas> <mx:Button id="myButton" creationComplete="dispatchEvent(new Event("Foo"))"/></mx:Canvas
<view:MyComponent creationComplete="facade.registerMediator();"/>
<mx:Canvas> <mx:Button id="myBtn"/></mx:Canvas>
<view:MyComponent id="myC" creationComplete="dispatchEvent(new Event("mycreated"));"/>
...public function MainMediator(vc:Main){ super(NAME, vc); (viewComponent as Main).addEventListener("mycreated", onComponentCreated); if((viewComponent as Main).myC) facade.registerMediator(new MyComponentMediator((viewComponent as Main).myC));}protected function onComponentCreated(e:Event):void{ if(!facade.hasMediator(MyComponentMediator.NAME)) { facade.registerMediator(new MyComponentMediator((viewComponent as Main).myC)); }}
...override public function onRegister():void{ // THIS IS THE CODE WHERE I GET A NULL REFERENCE ON myBtn (viewComponent as MyComponent).myBtn.addEventListener(MouseEvent.CLICK, onMouseClick);}