tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/847-refactoring-a-current-project-to-include-robot-legsRobotlegs: Discussion 2012-03-30T09:01:50Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/143529222012-03-10T11:02:28Z2012-03-10T11:02:28ZRefactoring a current project to include robot legs<div><p>Hi Nate,</p>
<blockquote>
<p>My problem is that this controller dispatches events. I'm not
sure how to keep dispatching events with this class and change it
to an actor. I see there's an IEventDispatcher on it. Should I
create an instance of EventDispatcher and populate that? I think
that's wrong because I'm guessing it will be fulfilled when I
inject it.</p>
</blockquote>
<p>I assume you have something like this in your controller:<br>
<strong>dispatchEvent</strong>(new
SomeCustomEvent(SomeCustomEvent.SOME_CUSTOM_TYPE, value));</p>
<p>If you’d extend Actor you’d only have to replace
<em>dispatchEvent</em> with <em>dispatch</em> in your code:<br>
<strong>dispatch</strong>(new
SomeCustomEvent(SomeCustomEvent.SOME_CUSTOM_TYPE,value));</p>
<p>But, if you want to use “dispatchEvent” instead of
“dispatch”, you can create your own „Actor“
like this:</p>
<pre>
<code>
import flash.events.Event;
import flash.events.IEventDispatcher;
public class BaseDispatcher
{
[Inject]
public var eventDispatcher:IEventDispatcher;
protected function dispatchEvent(event:Event):void
{
if (eventDispatcher.hasEventListener(event.type))
eventDispatcher.dispatchEvent(event);
}
}</code>
</pre>
<p>Then:<br>
public class SomeDispatcher extends BaseDispatcher</p>
<p>dispatchEvent(new
SomeCustomEvent(SomeCustomEvent.SOME_CUSTOM_TYPE, value));</p>
<p>As you know, you can inject the IEventDispatcher wherever you
need it:</p>
<pre>
<code>
public class SomeDispatcher
{
public function SomeDispatcher()
{
}
protected var _eventDispatcher:IEventDispatcher;
public function get eventDispatcher():IEventDispatcher
{
return _eventDispatcher;
}
[Inject]
public function set eventDispatcher(value:IEventDispatcher):void
{
_eventDispatcher=value;
}
public function doDispatch(value:String):void
{
eventDispatcher.dispatchEvent(new SomeCustomEvent(SomeCustomEvent.SOME_CUSTOM_TYPE, value));
}
}</code>
</pre>
<p>Does this help?</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/143529222012-03-12T20:57:37Z2012-03-12T20:57:40ZRefactoring a current project to include robot legs<div><p>I'm a little confused.</p>
<p>If I change my "controller" class to an actor and replace
dispatchEvent with dispatch, this breaks all event listeners on
that class. If I did this, i would have to re-write my app to use
the RL event dispatching.</p>
<p>Is this correct?</p></div>Natetag:robotlegs.tenderapp.com,2009-10-18:Comment/143529222012-03-13T12:56:24Z2012-03-13T22:40:03ZRefactoring a current project to include robot legs<div><p>Hey Nate,</p>
<p>I thought your intention was to refactor your
“controller” so you can take advantage of the shared
even dispatcher provided by robotlegs. I think changes to other
parts of your app are unavoidable, if you want to do it à la
robotlegs :)</p>
<p>A temporary solution, until you define your new classes, that
should replace the huge controller:</p>
<p>SomeDispatcher (from my previous post) would be your
“controller” class. You provide it with an
IEventDispatcher, so you can communicate with other rl classes.
Have 2 methods: addEventListener and dispatchEvent, so you can add
event listeners to it.</p>
<p>Mapping:<br>
injector.mapSingleton(SomeDispatcher);</p>
<p><strong>SomeDispatcher.as</strong></p>
<pre>
<code>
import flash.events.Event;
import flash.events.IEventDispatcher;
public class SomeDispatcher
{
protected var _eventDispatcher:IEventDispatcher;
public function SomeDispatcher()
{
}
public function get eventDispatcher():IEventDispatcher
{
return _eventDispatcher;
}
[Inject]
public function set eventDispatcher(value:IEventDispatcher):void
{
_eventDispatcher=value;
}
public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
{
eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
}
public function dispatchEvent(event:Event):void
{
eventDispatcher.dispatchEvent(event);
}
}</code>
</pre>
<p>Now, you can inject SomeDispatcher in SomeMediator and let
someDispatcher dispatch an event on the shared event
dispatcher.</p>
<p>SomeMediator.as</p>
<pre>
[Inject] public var view:SomeView;
[Inject] public var someDispatcher:SomeDispatcher;
override public function onRegister():void
{
view.someDispatcher=someDispatcher;
}
protected function onSomethingHappened():void
{
someDispatcher.dispatchEvent(new SomeCustomEvent(SomeCustomEvent.SOME_CUSTOM_TYPE, "from SomeMediator");
}
</pre>
<p>AnotherMediator.as has registered a listener for the event
dispatched by someDispatcher:</p>
<pre>
<code>
override public function onRegister():void
{
eventMap.mapListener(eventDispatcher, SomeCustomEvent.SOME_CUSTOM_TYPE, onSomeCustomEvent);
}
protected function onSomeCustomEvent(event:SomeCustomEvent):void
{
trace(event.someMessage);//would trace "from SomeMediator"
}</code>
</pre>
<blockquote>
<p>I pass that controller around to different parts of the app,
mostly view components to interact with it.</p>
</blockquote>
<p>As you can see above, SomeMediator passes the injected
someDispatcher to SomeView.<br>
In SomeView:</p>
<pre>
<code>
public var someDispatcher:SomeDispatcher;
private function initView():void
{
someDispatcher.addEventListener(SomeCustomEvent.SOME_CUSTOM_TYPE, actionHandler);
}
private function someViewList_changeHandler(event:IndexChangeEvent):void
{
someDispatcher.dispatchEvent(new SomeCustomEvent(SomeCustomEvent.SOME_CUSTOM_TYPE, "from SomeView"));
}
private function actionHandler(event:SomeCustomEvent):void
{
trace(event.type+" "+event.someMessage);//traces from SomeView
}</code>
</pre>
<p>As you can see, you can use the old syntax
someDispatcher.dispatchEvent and
someDispatcher.addEventListener.<br>
But, as a side effect, the events dispatched by someDispatcher in
SomeView will be heard by AnotherMediator as well, which is not
exactly how rl-MVCS should work. Therefore that should be just a
temporary solution!!!<br>
Does that help?<br>
Others may chime in with a better solution(?)</p>
<p>Ondina</p></div>Ondina D.F.