How do I map an event to a views button

Joshua Cave's Avatar

Joshua Cave

02 Apr, 2013 10:39 PM

Hi guys Im new to robotlegs. I think im getting the hang of it, so far Ive figured out how to map an view to mitigator, create a custom event, creating a command that listens for that event and fires the events payload off to a model or service from there a service can dispatch an event back to the view. However im confused about a few things. How can i just fire an event off from a button press on a view. i.e. i press say a login in button on the view. In the mitigator is there a way to map this straight to an event. I could do it as follows but i think there is a better way that Im not quite getting.

        override public function onRegister():void
        {
            eventMap.mapListener(view.employeeLoginButton, MouseEvent.CLICK, empLoginClicked);
}
private function empLoginClicked(e:MouseEvent):void { dispatch(LoginEvent);
}
  1. Support Staff 1 Posted by Ondina D.F. on 03 Apr, 2013 07:51 AM

    Ondina D.F.'s Avatar

    Hi Joshua,

    Hehe, Mitigator! Thanks for putting a smile on my face :)

    The better way:

    LoginView:

    private function employeeLoginButtonClickHandler(event:MouseEvent):void
    {
        loginVO.user_name = user_name.text;
        dispatchEvent(new LoginEvent(LoginEvent.LOGIN_REQUESTED, loginVO));
    }
    

    LoginMediator

    override public function onRegister():void
    {
        addViewListener(LoginEvent.LOGIN_REQUESTED, dispatch, LoginEvent);
    }
    

    LoginEvent

    private var _loginVO:LoginVO;
    
    public function LoginEvent (type:String, loginVO:LoginVO, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        _loginVO = loginVO;
        super(type, bubbles, cancelable);
    }
    
    public function get loginVO():LoginVO
    {
        return _loginVO;
    }
    
    override public function clone():Event
    {
        return new LoginEvent (type, loginVO);
    }
    

    LoginEvent overrides clone(), so your mitigator can redispatch it, either like this:

    addViewListener(LoginEvent.LOGIN_REQUESTED, dispatch, LoginEvent);
    

    or like this:

    addViewListener(LoginEvent.LOGIN_REQUESTED, onLoginRequested, LoginEvent);
    
    private function onLoginRequested (event:LoginEvent):void
    {
        dispatch(event);
    }
    

    Does this help?
    Ondina

  2. 2 Posted by Joshua Cave on 06 Apr, 2013 10:43 AM

    Joshua Cave's Avatar

    This worked perfectly.. Thanks for your reply.. and yes the mediator made me laugh too because im pretty sure I called it a mitigator in front of my supervisor who is guiding/grooming me for a junior role. headsmack he probably knew i meant mediatior...

  3. Support Staff 3 Posted by Ondina D.F. on 08 Apr, 2013 08:21 AM

    Ondina D.F.'s Avatar

    Hey Joshua,

    You’re welcome.
    I’m sure your supervisor liked it. Mitigator sounds cool and original ;) It also adds a new perspective to the roles of Mediators. It alleviates Views’ situation: Views become well encapsulated, flexible, framework-agnostic, portable and reusable.
    If I were you, I’d name my mediators SomeMitigator, AnotherMitigator, LoginMitigator, just for the fun of it.

    Closing this discussion for now.

    Ondina

  4. Ondina D.F. closed this discussion on 08 Apr, 2013 08:21 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