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

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

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