How do I Access my View Component in a Mediator?
When a View Component is added to the stage within a Context’s contextView, it is by default mediated automatically based on configuration supplied to the MediatorMap when the mapping was made. In a basic mediator, the viewComponent property is injected with the view component that is being mediated. A Mediator’s viewComponent property is of type Object. In most cases, we want access to a strongly typed object to receive the benefits provided by using strongly typed objects. To achieve this, we inject the typed instance of the view component that is being mediated:
public class GalleryLabelMediator extends Mediator implements IMediator
{
[Inject]
public var myCustomComponent:MyCustomComponent;
/**
* overriding the onRegister method is a good chance to
* add any system or View Component Events the Mediator
* is interested in receiving.
*/
override public function onRegister():void
{
//adding an event listener to the Context for framework events
addEventListenerTo( eventDispatcher, MyCustomEvent.DO_STUFF, handleDoStuff );
//adding an event listener to the view component being mediated
addEventListenerTo( myCustomComponent, MyCustomEvent.DID_SOME_STUFF, handleDidSomeStuff)
}
protected function handleDoStuff(event:MyCustomEvent):void
{
//setting a property on the view component from the
//strongly typed event payload. The view component
//will likely manage its own state based on this
//new data.
myCustomComponent.aProperty = event.payload
}
protected function handleDidSomeStuff(event:MyCustomEvent):void
{
//relaying the event to the framework
dispatch(event)
}
}
Following this approach we now have easy direct access to the public properties and methods of the mediated view component.