Compiled AIR app: Error #1009 when dispatching events from mediators

jonasnys's Avatar

jonasnys

26 Oct, 2011 08:01 AM

When running a compiled AIR application I get 1009 errrors when dispatching events from the mediators.
Is that problem familiar to anyone? Known solution?

I'm using Flex sdk 4.5.1 and latest versions of RL/SS. Working on a windows machine.

Typical button click handler from a test mediator causing this error when compiled:

    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
        } 
    }
  1. Support Staff 1 Posted by Ondina D.F. on 26 Oct, 2011 01:04 PM

    Ondina D.F.'s Avatar

    Hi Jonas,

    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:
    eventMap.mapListener(view.someButton, MouseEvent.CLICK, onBtnClick);

    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 #1009 already when the mediator would try to map the event.
    In this case your onBtnClick() method would never be called and the new Event(‘TEST’) never dispatched, of course.

    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.

    I’m using FlashBuilder 4.5.1 AIR 3 SDK on Windows , robotlegs-framework-v1.5.2.swc.
    I’ve tried your code and it worked:

      
    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");
    }
    }
    

    You don’t need to use the dispatcher like this: this.eventDispatcher.dispatchEvent(new Event('TEST'));
    You can simply use this convenience method:
    dispatch(new Event('TEST'));

    You also could do something like this:
    SomeView.mxml
    import yourpath.events.SomeEvent;
    private function onSomeButtonClicked():void
    {

    dispatchEvent(new SomeEvent(SomeEvent.DO_SOMETHING, payload));

    }

    SomeMediator.as

    
    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);
    }
    }
    

    addViewListener(SomeEvent.DO_SOMETHING, dispatch);
    is the same as:
    eventMap.mapListener(view, SomeEvent.DO_SOMETHING, dispatch);

    or as:
    override public function onRegister():void
    {

    eventMap.mapListener(view, SomeEvent.DO_SOMETHING, onDoSomething);

    }

    protected function onDoSomething (event: SomeEvent):void
    {

    dispatch(event);

    }

    I hope this helps.
    Ondina

  2. 2 Posted by jonasnys on 26 Oct, 2011 02:00 PM

    jonasnys's Avatar

    Thank you, Ondina!

    It seems that the problem was caused because I didn't include the Inject and PostConstruct compiler directives... Now it works like a charm!

  3. Support Staff 3 Posted by Ondina D.F. on 26 Oct, 2011 03:44 PM

    Ondina D.F.'s Avatar

    Aah!! Now, it’s clear to me that my first question should have been:
    “Are you linking against the source? If so, then add
    -keep-as3-metadata+=Inject -keep-as3-metadata+=PostConstruct to the compiler arguments”,
    because you were referring to the “latest versions of RL/SS”, robotlegs and swiftsuspenders!!
    I just assumed you were using the robotlegs swc.
    My bad:)

  4. Ondina D.F. closed this discussion on 01 Nov, 2011 11:45 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac