tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/1142-can-i-use-mapevent-with-uncaughterroreventuncaught_errorRobotlegs: Discussion 2012-10-15T08:39:58Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/195349512012-10-11T10:09:10Z2012-10-11T10:09:10ZCan I use mapEvent with UncaughtErrorEvent.UNCAUGHT_ERROR?<div><p>Hi,<br>
I was wondering if there is a way I can listen for global errors in
my app using flash's UncaughtErrorEvent.UNCAUGHT_ERROR event?<br>
It needs to be listening to the loaderInfo like so:</p>
<p>loaderInfo.uncaughtErrorEvents.addEventListener(
UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);</p>
<p>How could I do this within the Robotlegs framework?</p>
<p>Cheers,<br>
Tim</p></div>Timtag:robotlegs.tenderapp.com,2009-10-18:Comment/195349512012-10-11T10:10:26Z2012-10-11T10:10:26ZCan I use mapEvent with UncaughtErrorEvent.UNCAUGHT_ERROR?<div><p>P.S.<br>
I assume adding this in my MainContext won't work:<br>
commandMap.mapEvent(UncaughtErrorEvent.UNCAUGHT_ERROR,
DisplayErrorCommand);</p></div>Timtag:robotlegs.tenderapp.com,2009-10-18:Comment/195349512012-10-11T10:53:03Z2012-10-11T10:53:03ZCan I use mapEvent with UncaughtErrorEvent.UNCAUGHT_ERROR?<div><p>Hi Tim,</p>
<p>„ Commands are only executed in response to framework
events“</p>
<p>Details here:</p>
<p><a href=
"https://github.com/robotlegs/robotlegs-framework/wiki/best-practices#wiki-commands">
https://github.com/robotlegs/robotlegs-framework/wiki/best-practice...</a></p>
<p>In the uncaughtErrorHandler you should dispatch a custom event
(say DisplayErrorEvent) with UncaughtErrorEvent’s event.error
as a payload.</p>
<pre>
<code>private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
dispatchEvent(new DisplayErrorEvent(DisplayErrorEvent.SOME_ERROR, event.error));
}</code>
</pre>
<p>Mapping the custom event:</p>
<pre>
<code>commandMap.mapEvent(DisplayErrorEvent.SOME_ERROR, DisplayErrorCommand);</code>
</pre>
<p>If your Loader is in a View, then the Mediator of this View
should listen for DisplayErrorEvent and redispatch it, in order to
trigger DisplayErrorCommand.</p>
<p>You’ll have to inject DisplayErrorEvent into your
DisplayErrorCommand, so you can use its payload.</p>
<p>Does this help?</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/195349512012-10-11T11:16:36Z2012-10-11T11:17:45ZCan I use mapEvent with UncaughtErrorEvent.UNCAUGHT_ERROR?<div><p>Thanks Ondina,</p>
<p>That sounds like a good solution - so in the view I would listen
for the actual Flash event then in its view mediator I would
re-dispatch it into the RobotLegs framework event chain something
like:</p>
<p>override public function onRegister():void<br>
{</p>
<p>view.addEventListener(DisplayErrorEvent.SOME_ERROR,
onUnCaughtError, false);</p>
<p>}</p>
<p>private function onUnCaughtError(e:DisplayErrorEvent):void</p>
<pre>
<code> {
//re-dispatch event</code>
</pre>
<p>eventDispatcher.dispatchEvent(e);</p>
<pre>
<code> }</code>
</pre></div>Timtag:robotlegs.tenderapp.com,2009-10-18:Comment/195349512012-10-11T11:40:51Z2012-10-11T11:40:51ZCan I use mapEvent with UncaughtErrorEvent.UNCAUGHT_ERROR?<div><p>My pleasure, Tim.</p>
<blockquote>
<p>so in the view I would listen for the actual Flash event then in
its view mediator I would re-dispatch it</p>
</blockquote>
<p>Exactly.</p>
<p>My suggestion is to use the eventMap to map the listeners in
your Mediator:<br>
- with a handler like in your example</p>
<pre>
<code>override public function onRegister():void
{
eventMap.mapListener(view, DisplayErrorEvent.SOME_ERROR, onUnCaughtError, DisplayErrorEvent);
//or:
// addViewListener(DisplayErrorEvent.SOME_ERROR, onUnCaughtError, DisplayErrorEvent);
}</code>
</pre>
<p>-without a handler method (the event will be redispatched
automatically)</p>
<p>eventMap.mapListener(view, DisplayErrorEvent.SOME_ERROR,
<strong>dispatch</strong>, DisplayErrorEvent);</p>
<p>More details about the advantages of using the EventMap in my
response here:</p>
<p><a href=
"http://knowledge.robotlegs.org/discussions/problems/644-events-being-caught-intermittently#comment_18891496">
http://knowledge.robotlegs.org/discussions/problems/644-events-bein...</a></p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/195349512012-10-11T13:24:21Z2012-10-11T13:24:41ZCan I use mapEvent with UncaughtErrorEvent.UNCAUGHT_ERROR?<div><p>Thanks again Ondina.<br>
I didn't realise you could use the eventMap.mapListener with a view
instance as the first argument - I have always used it as
addViewListener for listening to view events and the
eventMap.mapListener for listening for other "external" events to
the view.</p>
<p>What I have now is in my view class:</p>
<pre>
<code>public function MainView()
{
loaderInfo.uncaughtErrorEvents.addEventListener( UncaughtErrorEvent.UNCAUGHT_ERROR, onUnCaughtErrorHandler);
}
protected function onUnCaughtErrorHandler(event:UncaughtErrorEvent):void
{
var errorEvent:AppEvent = new AppEvent(AppEvent.STATUS_ERROR);
errorEvent.messageStr = "Uncaught error - id: "+ event.errorID;
this.dispatchEvent(errorEvent);
}</code>
</pre>
<p>And then in its mediator class</p>
<pre>
<code>override public function onRegister():void
{
//Listen for uncaught errors
eventMap.mapListener(view, AppEvent.STATUS_ERROR, onUnCaughtError, AppEvent);
}
private function onUnCaughtError(e:AppEvent):void
{
// Re-dispatch event
dispatch(e);
}</code>
</pre>
<p>Or do I need to actually re-dispatch the event literally in the
mediator's handler using a new AppEvent instance (I assume this
might clone the event rather than passing the same one along)?</p></div>Timtag:robotlegs.tenderapp.com,2009-10-18:Comment/195349512012-10-11T14:40:59Z2012-10-11T14:40:59ZCan I use mapEvent with UncaughtErrorEvent.UNCAUGHT_ERROR?<div><p>You’re welcome, again.</p>
<blockquote>
<p>Or do I need to actually re-dispatch the event literally in the
mediator's handler using a new AppEvent instance (I assume this
might clone the event rather than passing the same one along)?</p>
</blockquote>
<p>Nope. If your custom event overrides clone(), then it will be
re-dispatched.</p>
<p>I’m sure you know how to create a custom event class, but
just for the sake of completeness I’m adding a code snippet
:)</p>
<p>Custom event:</p>
<pre>
<code>public class SomeErrorEvent extends Event
{
public static const STATUS_ERROR:String = "statusError";
public function SomeErrorEvent(type:String, someMessage:String, bubbles:Boolean = false, cancelable:Boolean = false)
{
super(type, bubbles, cancelable);
_someMessage = someMessage;
}
private var _someMessage:String;
public function get someMessage():String
{
return _someMessage;
}
override public function clone():Event
{
return new SomeErrorEvent(type, someMessage, bubbles, cancelable);
}
}</code>
</pre>
<p>In your View:</p>
<pre>
<code>protected function onUnCaughtErrorHandler(event:UncaughtErrorEvent):void
{
var errorMessage:String= "Uncaught error - id: "+ event.errorID;
dispatchEvent(new SomeErrorEvent(SomeErrorEvent.STATUS_ERROR, errorMessage));
}</code>
</pre>
<p>In your Mediator, as I said in my previous post, you can do just
this:<br>
1.</p>
<pre>
<code>eventMap.mapListener(view, SomeErrorEvent.STATUS_ERROR, dispatch, SomeErrorEvent);</code>
</pre>
<p>or its equivalent:</p>
<pre>
<code>addViewListener(SomeErrorEvent.STATUS_ERROR, dispatch, SomeErrorEvent);</code>
</pre>
<p>or, as you did:<br>
2.</p>
<pre>
<code>eventMap.mapListener(view, SomeErrorEvent.STATUS_ERROR, onUnCaughtError, SomeErrorEvent);</code>
</pre>
<p>or its equivalent:</p>
<pre>
<code>addViewListener(SomeErrorEvent.STATUS_ERROR, onUnCaughtError, SomeErrorEvent);</code>
</pre>
<pre>
<code>private function onUnCaughtError(event: SomeErrorEvent):void
{
dispatch(event);
}</code>
</pre>
<p>Take a look at <a href=
"https://github.com/robotlegs/robotlegs-framework/blob/master/src/org/robotlegs/mvcs/Mediator.as">
https://github.com/robotlegs/robotlegs-framework/blob/master/src/or...</a><br>
to see addViewListener and addContextListener as syntactical sugar
for mapping listeners.</p>
<p>Did I answer all your questions?</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/195349512012-10-12T11:45:23Z2012-10-12T11:45:51ZCan I use mapEvent with UncaughtErrorEvent.UNCAUGHT_ERROR?<div><p>Hi Ondina,<br>
Thanks for explaining it so clearly. Yes I do know how to create
custom events but thanks for the refresher.</p>
<p>I had missed the bit about using dispatch in your
eventMap.mapListener - very handy. My way is the long way! ;)</p>
<p>About RobotLegs 2 - would you recommend using it yet or is there
a steep learning curve? Where can I find some good information on
getting started or is not quite ready yet?</p>
<p>Cheers,<br>
Tim</p></div>Tim Johnsontag:robotlegs.tenderapp.com,2009-10-18:Comment/195349512012-10-12T12:48:09Z2012-10-12T12:48:09ZCan I use mapEvent with UncaughtErrorEvent.UNCAUGHT_ERROR?<div><p>Hey Tim!</p>
<blockquote>
<p>About RobotLegs 2 - would you recommend using it yet or is there
a steep learning curve?</p>
</blockquote>
<p>I’ve ported 3 big projects from rl1 to rl2 already, and I
also have a few new projects, where I used rl2 from the start.<br>
All of them work without problems.<br>
I don’t use all rl2’s new features, but the ones I was
most interested in, Modular and Popups, are really a win.</p>
<p>So, yes, I recommend using robotlegs 2 :)</p>
<blockquote>
<p>Where can I find some good information on getting started or is
not quite ready yet?</p>
</blockquote>
<p>Go through the readme files under each folder:<br>
<a href=
"https://github.com/robotlegs/robotlegs-framework/tree/version2/src">
https://github.com/robotlegs/robotlegs-framework/tree/version2/src</a></p>
<p>Look at the examples:<br>
<a href=
"http://knowledge.robotlegs.org/discussions/examples/20-links-to-robotlegs-v2-articles-examples-demos-utilities-and-tutorials">
http://knowledge.robotlegs.org/discussions/examples/20-links-to-rob...</a></p>
<p>Try to build a simple application following the examples.</p>
<p>When in doubt, ask questions in here (actually under Robotlegs
2).</p>
<p>Read through the Robotlegs 2 discussions.</p>
<p>Cheers,<br>
Ondina</p></div>Ondina D.F.