tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/161-views-and-mediatorsRobotlegs: Discussion 2018-10-18T16:35:16Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/27909712010-09-02T14:08:46Z2010-09-16T12:30:10ZViews and Mediators<div><p>Hi All,</p>
<p>How do people go about not using custom events to dispatch events from their Views? My thinking is for the best re usability of the View I should not have it using custom events, just the native ones and then let the Mediator handle it.</p>
<p>ie.</p>
<p>in a View:</p>
<p>public function myFuntion():void<br />
{
myButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);<br />
}</p>
<p>private function buttonClickHandler(event:MouseEvent):void<br />
{
disptatchEvent(event);<br />
}</p>
<p>The the Mediator handles what to do with the event. I have tried this approach and have problems with it, or I may just be doing something wrong.</p>
<p>Thanks for reading, looking forward to hearing peoples thoughts.</p>
<p>Jono</p></div>Jonotag:robotlegs.tenderapp.com,2009-10-18:Comment/27909712010-09-02T14:20:47Z2010-09-02T14:20:47ZViews and Mediators<div><p>Hi Jono,</p>
<p>I use as3Signals to communicate between the view and the mediator, which keeps framework code out of the views (as you say, this is desirable) and avoids mediators having to drill down into the view or the view having to expose internal workings.</p>
<p>If you're not familiar with as3signals (Robert Penner) then go check them out - awesome!</p>
<p>Then I end up with code like:</p>
<p>public function myFuntion():void<br />
{
enterButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);<br />
}</p>
<p>private function buttonClickHandler(event:MouseEvent):void<br />
{</p>
<pre><code>enterButtonClickedSignal.dispatch();</code></pre>
<p>}</p>
<p>And in the mediator onRegister</p>
<pre><code>view.enterButtonClickedSignal.add(enterButtonSignalHandler);</code></pre>
<p>and so on.</p>
<p>Hope that helps,</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/27909712010-09-02T17:09:28Z2010-09-02T17:09:29ZViews and Mediators<div><p>Doesn't this add overhead another framework dispatching events, when you start thinking of optimization and performance for Flash Player 10.1 you want to limit events and dispatching of events how will this be affected by using as3Signals? <br />
</p></div>Almogtag:robotlegs.tenderapp.com,2009-10-18:Comment/27909712010-09-02T21:35:31Z2010-09-02T21:35:31ZViews and Mediators<div><p>I'll take a crack at it. To answer your question, yes, signals will<br />
add in a bit of overhead if you go that route. But it's a balancing<br />
act between less overhead or a less-coupled architecture.<br />
Fortunately, AS3Signals has a low overhead.</p>
<p>If you don't want to add the overhead, doing this in your mediator is an option:</p>
<p>eventMap.mapListener(<br />
</p>
<pre><code> view.loginButton,
MouseEvent.CLICK,
loginButton_clickHandler);</code></pre>
<p>protected function loginButton_clickHandler(event:Event):void<br />
</p>
<pre><code> {
eventDispatcher.dispatchEvent(new LoginEvent(
LoginEvent.LOG_IN,
view.username,
view.password));
}</code></pre>
<p>But like Stray said, that opens up some of the internals of your view.</p>
<p>Aaron</p></div>Aaron Hardytag:robotlegs.tenderapp.com,2009-10-18:Comment/27909712010-09-03T00:03:30Z2010-09-03T00:03:31ZViews and Mediators<div><p>Thanks I don't like the ideas of having events in my view, I'm new to Robotlegs and still learning what are the best practices for this?<br />
</p></div>Almogtag:robotlegs.tenderapp.com,2009-10-18:Comment/27909712010-09-03T00:45:35Z2010-09-03T00:46:20ZViews and Mediators<div><p>Quote @Jono: "I have tried this approach and have problems with it, or I may just be doing something wrong."<br />
What are the exact problems you're having with it? If it's not working, make sure you set <code>bubbles</code> to <code>true</code> when you (re)dispatch the event.<br />
If it's about design, here's what I'd do:</p>
<p>My view component:</p>
<p>` class MyCustomComponent extends Sprite
{
public static const CLICKED:String = "clicked";</p>
<p> [...]</p>
<p> private function onClick(event:MouseEvent):void
{</p>
<pre><code>dispatchEvent(new Event(CLICKED, true));</code></pre>
<p> }
} `</p>
<p>My Mediator:</p>
<p>` class MyCustomMediator extends Mediator
{
override public function onRegister():void
{</p>
<pre><code>addViewListener(MyCustomComponent.CLICKED, component_onClicked);</code></pre>
<p> }</p>
<p> private function component_onClicked(event:Event):void
{</p>
<pre><code>// Events:
dispatch(new MyCustomEvent(MyCustomEvent.CLICKED));
// Signals:
myCustomSignal.dispatch();</code></pre>
<p> }
} `</p>
<p>@Almog: <a href="http://wiki.github.com/robotlegs/robotlegs-framework/best-practices">http://wiki.github.com/robotlegs/robotlegs-framework/best-practices</a></p>
<p>[EDIT: I don't know why the code layout is so weird.]</p></div>Abel de Beertag:robotlegs.tenderapp.com,2009-10-18:Comment/27909712010-09-06T05:21:01Z2010-09-06T05:21:02ZViews and Mediators<div><p>Thanks for the advice everyone - I'll try out bubbling and signals and see which option I like more. Perhaps post some details on how I find them.</p></div>Jonotag:robotlegs.tenderapp.com,2009-10-18:Comment/27909712010-09-21T15:31:23Z2010-09-21T15:31:24ZViews and Mediators<div><p>@abeldebeer - Ya that's what I've been doing as well, define some string constants within your view, and then the mediator can listen for those custom events.</p>
<p>This gives you good way to expose the events a view dispatches, without creating any external dependencies (including signals).</p></div>Shawn Blais