How do I access the stage from a Mediator?

Darryl Wright's Avatar

Darryl Wright

02 Mar, 2017 03:12 AM

I was originally registering for mouse licks in my mediator on the associated view. Now we want to just simply capture all mouse clicks on a page (regardless of control you're over). My understanding is this is accomplished by:

stage.addEventListener(MouseEvent.CLICK, onTargetClick);

I do not have stage in my mediator and I am not sure how to get it. Or is there a better way to get "global" events from within the Mediator?

Thanks,
Darryl

  1. Support Staff 1 Posted by Ondina D.F. on 06 Mar, 2017 03:48 PM

    Ondina D.F.'s Avatar

    Hey Darryl,

    For some reasons, your message has landed in the 'spam' folder of this forum. I restored it today.

    This would be one solution:

    [Inject]
    public var view:YourView;
    
    override public function initialize():void
    {
        view.stage.addEventListener(MouseEvent.CLICK, onTargetClick); 
    }
    //in case the view is going to be removed from stage::
    override public function destroy():void
    {
        view.stage.removeEventListener(MouseEvent.CLICK, onTargetClick);
        super.destroy();
    }
    

    If the view is going to be removed from stage at some point, you'd need to explicitly remove the listener inside of the destroy() method, as shown above. The reason for that is that a mediator which keeps a reference to its view can't be destroyed.

    Another solution would be to let the view listen for the MouseEvent and in the handler of the event dispatch a custom event to which the mediator would listen:

    override public function initialize():void
    {
        addViewListener(SomeViewEvent.GLOBAL_CLICK, onTargetClick);
    }
    

    In this case, you don't need to remove the event listener inside of destroy(), the mediator will take care of it automatically.

    I'd prefer the second solution.

    I hope this helps.
    Ondina

  2. Ondina D.F. closed this discussion on 13 Apr, 2017 02:34 PM.

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