Views and Mediators

Jono's Avatar

Jono

02 Sep, 2010 02:08 PM

Hi All,

How do people go about not using custom events to dispatch events from their Views? My thinking is for the best re usability of the View I should not have it using custom events, just the native ones and then let the Mediator handle it.

ie.

in a View:

public function myFuntion():void
{ myButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
}

private function buttonClickHandler(event:MouseEvent):void
{ disptatchEvent(event);
}

The the Mediator handles what to do with the event. I have tried this approach and have problems with it, or I may just be doing something wrong.

Thanks for reading, looking forward to hearing peoples thoughts.

Jono

  1. 1 Posted by Stray on 02 Sep, 2010 02:20 PM

    Stray's Avatar

    Hi Jono,

    I use as3Signals to communicate between the view and the mediator, which keeps framework code out of the views (as you say, this is desirable) and avoids mediators having to drill down into the view or the view having to expose internal workings.

    If you're not familiar with as3signals (Robert Penner) then go check them out - awesome!

    Then I end up with code like:

    public function myFuntion():void
    {
    enterButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
    }

    private function buttonClickHandler(event:MouseEvent):void
    {
    enterButtonClickedSignal.dispatch();
    }

    And in the mediator onRegister

    view.enterButtonClickedSignal.add(enterButtonSignalHandler);

    and so on.

    Hope that helps,

    Stray

  2. 2 Posted by Almog on 02 Sep, 2010 05:09 PM

    Almog's Avatar

    Doesn't this add overhead another framework dispatching events, when you start thinking of optimization and performance for Flash Player 10.1 you want to limit events and dispatching of events how will this be affected by using as3Signals?

  3. 3 Posted by Aaron Hardy on 02 Sep, 2010 09:35 PM

    Aaron Hardy's Avatar

    I'll take a crack at it. To answer your question, yes, signals will
    add in a bit of overhead if you go that route. But it's a balancing
    act between less overhead or a less-coupled architecture.
    Fortunately, AS3Signals has a low overhead.

    If you don't want to add the overhead, doing this in your mediator is an option:

    eventMap.mapListener(
                                           view.loginButton,
                                           MouseEvent.CLICK,
                                           loginButton_clickHandler);

    protected function loginButton_clickHandler(event:Event):void
                   {
                           eventDispatcher.dispatchEvent(new LoginEvent(
                                           LoginEvent.LOG_IN,
                                           view.username,
                                           view.password));
                   }

    But like Stray said, that opens up some of the internals of your view.

    Aaron

  4. 4 Posted by Almog on 03 Sep, 2010 12:03 AM

    Almog's Avatar

    Thanks I don't like the ideas of having events in my view, I'm new to Robotlegs and still learning what are the best practices for this?

  5. 5 Posted by Abel de Beer on 03 Sep, 2010 12:45 AM

    Abel de Beer's Avatar

    Quote @Jono: "I have tried this approach and have problems with it, or I may just be doing something wrong."
    What are the exact problems you're having with it? If it's not working, make sure you set bubbles to true when you (re)dispatch the event.
    If it's about design, here's what I'd do:

    My view component:

    ` class MyCustomComponent extends Sprite { public static const CLICKED:String = "clicked";

    [...]

    private function onClick(event:MouseEvent):void {

    dispatchEvent(new Event(CLICKED, true));

    } } `

    My Mediator:

    ` class MyCustomMediator extends Mediator { override public function onRegister():void {

    addViewListener(MyCustomComponent.CLICKED, component_onClicked);

    }

    private function component_onClicked(event:Event):void {

    // Events:
    dispatch(new MyCustomEvent(MyCustomEvent.CLICKED));
    
    // Signals:
    myCustomSignal.dispatch();

    } } `

    @Almog: http://wiki.github.com/robotlegs/robotlegs-framework/best-practices

    [EDIT: I don't know why the code layout is so weird.]

  6. 6 Posted by Jono on 06 Sep, 2010 05:21 AM

    Jono's Avatar

    Thanks for the advice everyone - I'll try out bubbling and signals and see which option I like more. Perhaps post some details on how I find them.

  7. Jono closed this discussion on 16 Sep, 2010 02:05 PM.

  8. Shawn Blais re-opened this discussion on 21 Sep, 2010 03:31 PM

  9. 7 Posted by Shawn Blais on 21 Sep, 2010 03:31 PM

    Shawn Blais's Avatar

    @abeldebeer - Ya that's what I've been doing as well, define some string constants within your view, and then the mediator can listen for those custom events.

    This gives you good way to expose the events a view dispatches, without creating any external dependencies (including signals).

  10. Stray closed this discussion on 10 Feb, 2011 05:49 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