tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/410-compiled-air-app-error-1009-when-dispatching-events-from-mediatorsRobotlegs: Discussion 2013-04-28T09:54:27Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/108873782011-10-26T08:01:09Z2011-10-26T08:01:09ZCompiled AIR app: Error #1009 when dispatching events from mediators<div><p>When running a compiled AIR application I get 1009 errrors when
dispatching events from the mediators.<br>
Is that problem familiar to anyone? Known solution?</p>
<p>I'm using Flex sdk 4.5.1 and latest versions of RL/SS. Working
on a windows machine.</p>
<p>Typical button click handler from a test mediator causing this
error when compiled:</p>
<pre>
<code> protected function onBtnClick(event:MouseEvent):void
{
trace('onBtnClick');
try
{
this.eventDispatcher.dispatchEvent(new Event('TEST'));
}
catch(error:Error)
{
Alert.show(error.message); // <<<-------- Displays Error #1009 when compiled
}
}</code>
</pre></div>jonasnystag:robotlegs.tenderapp.com,2009-10-18:Comment/108873782011-10-26T13:04:17Z2011-10-26T13:04:17ZCompiled AIR app: Error #1009 when dispatching events from mediators<div><p>Hi Jonas,</p>
<p>One possibility could be that you have a button -let’s
call it someButton- in your view and in the mediator’s
onRegister you do something like this:<br>
eventMap.mapListener(view.someButton, MouseEvent.CLICK,
onBtnClick);</p>
<p>Let’s say you have 2 states in your view: stateOne and
stateTwo, where stateOne is the first state, but someButton is
available only in stateTwo. In this case the mediator won’t
be able to add an event listener to the button because someButton
is null in stateOne and you’d get a TypeError: Error <a href=
"/discussions/problems/1009" title="Discussion #1009">#1009</a>
already when the mediator would try to map the event.<br>
In this case your onBtnClick() method would never be called and the
new Event(‘TEST’) never dispatched, of course.</p>
<p>But if your onBtnClick() has been called, you should be able to
dispatch the new Event(‘TEST’) without any problems.
Are you sure the error doesn’t come from the eventMap? Maybe
you should paste the entire error in here.</p>
<p>I’m using FlashBuilder 4.5.1 AIR 3 SDK on Windows ,
robotlegs-framework-v1.5.2.swc.<br>
I’ve tried your code and it worked:</p>
<pre>
<code>
import flash.events.Event;
import flash.events.MouseEvent;
import mx.controls.Alert;
import org.robotlegs.mvcs.Mediator;
import yourpath.views.components.SomeView;
public class SomeMediator extends Mediator
{
[Inject]
public var view:SomeView;
override public function onRegister():void
{
eventMap.mapListener(view.someButton, MouseEvent.CLICK, onBtnClick);
addContextListener('TEST', onTest);
}
protected function onBtnClick(event:MouseEvent):void
{
try
{
dispatch(new Event('TEST'));
}
catch (error:Error)
{
Alert.show(error.message);
}
}
protected function onTest(event:Event):void
{
Alert.show("onTest");
}
}</code>
</pre>
<p>You don’t need to use the dispatcher like this:
this.eventDispatcher.dispatchEvent(new Event('TEST'));<br>
You can simply use this convenience method:<br>
dispatch(new Event('TEST'));</p>
<p>You also could do something like this:<br>
SomeView.mxml<br>
import yourpath.events.SomeEvent;<br>
private function onSomeButtonClicked():void<br>
{</p>
<p>dispatchEvent(new SomeEvent(SomeEvent.DO_SOMETHING,
payload));</p>
<p>}</p>
<p>SomeMediator.as</p>
<pre>
<code>
import org.robotlegs.mvcs.Mediator;
import yourpath.events.SomeEvent;
import yourpath.views.components.SomeView;
public class SomeMediator extends Mediator
{
[Inject]
public var view:SomeView;
override public function onRegister():void
{
//redispatching an Event
addViewListener(SomeEvent.DO_SOMETHING, dispatch);
}
}</code>
</pre>
<p>addViewListener(SomeEvent.DO_SOMETHING, dispatch);<br>
is the same as:<br>
eventMap.mapListener(view, SomeEvent.DO_SOMETHING, dispatch);</p>
<p>or as:<br>
override public function onRegister():void<br>
{</p>
<p>eventMap.mapListener(view, SomeEvent.DO_SOMETHING,
onDoSomething);</p>
<p>}</p>
<p>protected function onDoSomething (event: SomeEvent):void<br>
{</p>
<p>dispatch(event);</p>
<p>}</p>
<p>I hope this helps.<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/108873782011-10-26T14:00:44Z2011-10-26T14:00:44ZCompiled AIR app: Error #1009 when dispatching events from mediators<div><p>Thank you, Ondina!</p>
<p>It seems that the problem was caused because I didn't include
the Inject and PostConstruct compiler directives... Now it works
like a charm!</p></div>jonasnystag:robotlegs.tenderapp.com,2009-10-18:Comment/108873782011-10-26T15:44:11Z2011-10-26T15:44:11ZCompiled AIR app: Error #1009 when dispatching events from mediators<div><p>Aah!! Now, it’s clear to me that my first question should
have been:<br>
“Are you linking against the source? If so, then add<br>
-keep-as3-metadata+=Inject -keep-as3-metadata+=PostConstruct to the
compiler arguments”,<br>
because you were referring to the “latest versions of
RL/SS”, robotlegs <strong>and</strong> swiftsuspenders!!<br>
I just assumed you were using the robotlegs swc.<br>
My bad:)</p></div>Ondina D.F.