Mediate non-visual view components
In the definition of the Model-View-Controller pattern, the View
doesn't have to be represented by just visual components, it can
also be audio, for example. This is because all feedback or UI
elements that can be interpreted / interacted with by the user
belong to the View tier. So when talking about a view component
that just handles audio playback, it would be weird to have it
extend a DisplayObject class.
In Robotlegs when we talk about view components, we generally talk about visual objects. The framework has some built-in automation for the creation of Mediators for its view components based on whether an instance of the component is added to the stage. When talking about non-visual view components, obviously this behavior is obsolete. So how do we make sure our Mediators are registered correctly?
First, you map your view component class to the Mediator, but
pass in 'false' for the autoCreate and
autoRemove parameters:
mediatorMap.mapView(MyNonVisualView, MyNonVisualViewMediator,
null, false, false);
Note: the third argument, injectViewAs, can be ignored
for this example, so we pass in 'null'.
To instantiate the Mediator you ignore the
contextView, but instead do:
mediatorMap.createMediator(new MyNonVisualView());
This creates the Mediator, injects an instance of the view
component and calls the Mediator's onRegister()
method. You're good to go!
Note: to listen for and dispatch Events from your view
component, the view class should extend EventDispatcher. This way
you can still use the sweet eventMap and
addViewListener() helper methods from the Mediator
class.
Hope this is helpful to someone.
Comments are currently closed for this discussion. You can start a new one.
2 Posted by Nikos Katsikanis on 10 Jan, 2011 06:35 PM
good explanation
3 Posted by Todd Coulson on 25 Jan, 2011 09:09 PM
I tried this exact thing but I am getting an error I am unsure of:
mediatorMap.mapView(AudioPlayer,AudioPlayerMediator,null, false, false);
mediatorMap.createMediator(AudioPlayer);
AudioPlayer is a regular class that extends EventDispatcher, and AudioPlayerMediator obviously extends Mediator. But I am getting the following error:
TypeError: Error #1034: Type Coercion failed: cannot convert org.robotlegs.selfdirected.views.components.controlPanel::AudioPlayer$ to org.robotlegs.selfdirected.views.components.controlPanel.AudioPlayer.
Any idea what I am doing wrong with my non-visual component?
Thanks for this post, it is one of the only posts I could find on loading audio as a view.
4 Posted by Abel de Beer on 26 Jan, 2011 03:18 PM
@Todd: I'm not really sure what is going wrong here. Could you maybe post your code or attach a zip file of the project?
5 Posted by Todd Coulson on 26 Jan, 2011 04:33 PM
I really appreciate any eyes that can go on this. TIA.
6 Posted by Todd Coulson on 26 Jan, 2011 04:35 PM
Sorry I tried to post this, but it said I had not seen a comment and then took my attachment off.
7 Posted by Abel de Beer on 26 Jan, 2011 04:58 PM
Ahh yes, easy one:
mediatorMap.createMediator()expects an instance of the view component class, where you give it the Class. So just change that line in your context to:mediatorMap.createMediator(new AudioPlayer())Edit: I could have seen that from your first post BTW... But I didn't. ;) Oh and that means I typed my original post incorrectly as well! Will fix it now.
8 Posted by Todd Coulson on 26 Jan, 2011 05:36 PM
fabulous! makes perfect sense now, I thought it created an instance from the Class provided.
Stray closed this discussion on 16 Feb, 2011 04:57 PM.