tag:robotlegs.tenderapp.com,2009-10-18:/discussions/solutions/9-mediate-non-visual-view-componentsRobotlegs: Discussion 2018-10-18T16:35:21Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/46482002011-01-10T03:12:09Z2011-01-26T17:07:48ZMediate non-visual view components<div><p>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 <code>DisplayObject</code> class.</p>
<p>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?</p>
<p>First, you map your view component class to the Mediator, but
pass in 'false' for the <code>autoCreate</code> and
<code>autoRemove</code> parameters:
<code>mediatorMap.mapView(MyNonVisualView, MyNonVisualViewMediator,
null, false, false);</code><br>
Note: the third argument, <code>injectViewAs</code>, can be ignored
for this example, so we pass in 'null'.</p>
<p>To instantiate the Mediator you ignore the
<code>contextView</code>, but instead do:
<code>mediatorMap.createMediator(new MyNonVisualView());</code><br>
This creates the Mediator, injects an instance of the view
component and calls the Mediator's <code>onRegister()</code>
method. You're good to go!</p>
<p>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 <code>eventMap</code> and
<code>addViewListener()</code> helper methods from the Mediator
class.</p>
<p>Hope this is helpful to someone.</p></div>Abel de Beertag:robotlegs.tenderapp.com,2009-10-18:Comment/46482002011-01-10T18:35:33Z2011-01-10T18:35:33ZMediate non-visual view components<div><p>good explanation</p></div>Nikos tag:robotlegs.tenderapp.com,2009-10-18:Comment/46482002011-01-25T21:09:38Z2011-01-25T21:09:39ZMediate non-visual view components<div><p>I tried this exact thing but I am getting an error I am unsure
of:<br>
mediatorMap.mapView(AudioPlayer,AudioPlayerMediator,null, false,
false);<br>
mediatorMap.createMediator(AudioPlayer);<br>
AudioPlayer is a regular class that extends EventDispatcher, and
AudioPlayerMediator obviously extends Mediator. But I am getting
the following error:<br>
TypeError: Error <a href="/discussions/solutions/1034" title=
"Discussion #1034">#1034</a>: Type Coercion failed: cannot convert
org.robotlegs.selfdirected.views.components.controlPanel::AudioPlayer$
to
org.robotlegs.selfdirected.views.components.controlPanel.AudioPlayer.<br>
Any idea what I am doing wrong with my non-visual component?<br>
Thanks for this post, it is one of the only posts I could find on
loading audio as a view.</p></div>Todd Coulsontag:robotlegs.tenderapp.com,2009-10-18:Comment/46482002011-01-26T15:18:28Z2011-01-26T15:18:28ZMediate non-visual view components<div><p>@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?</p></div>Abel de Beertag:robotlegs.tenderapp.com,2009-10-18:Comment/46482002011-01-26T16:33:36Z2011-01-26T16:33:37ZMediate non-visual view components<div><p>I really appreciate any eyes that can go on this. TIA.</p></div>Todd Coulsontag:robotlegs.tenderapp.com,2009-10-18:Comment/46482002011-01-26T16:35:10Z2011-01-26T16:35:12ZMediate non-visual view components<div><p>Sorry I tried to post this, but it said I had not seen a comment
and then took my attachment off.</p></div>Todd Coulsontag:robotlegs.tenderapp.com,2009-10-18:Comment/46482002011-01-26T16:58:39Z2011-01-26T17:07:31ZMediate non-visual view components<div><p>Ahh yes, easy one:<br>
<code>mediatorMap.createMediator()</code> expects an
<em>instance</em> of the view component class, where you give it
the <em>Class</em>. So just change that line in your context to:
<code>mediatorMap.createMediator(new AudioPlayer())</code></p>
<p>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.</p></div>Abel de Beertag:robotlegs.tenderapp.com,2009-10-18:Comment/46482002011-01-26T17:36:43Z2011-01-26T17:36:43ZMediate non-visual view components<div><p>fabulous! makes perfect sense now, I thought it created an
instance from the Class provided.</p></div>Todd Coulson