Is context menu a mediator or view logic

Eli Atlas's Avatar

Eli Atlas

02 Dec, 2011 03:33 PM

I added a context menu to my example project.
And added the context menu logic to the mediator.
Should it be there, or in the the view component of this mediator?

Here is the class on GitHub:

  1. Support Staff 1 Posted by Ondina D.F. on 02 Dec, 2011 04:05 PM

    Ondina D.F.'s Avatar

    definitely in the view:)

  2. 2 Posted by Eli Atlas on 03 Dec, 2011 06:01 PM

    Eli Atlas's Avatar

    Hi Ondina

    Thanks for the answer.
    Can you explain a bit more?
    That's because mediator should contain only the logic related to the framework itself?

  3. Support Staff 3 Posted by Ondina D.F. on 04 Dec, 2011 03:01 PM

    Ondina D.F.'s Avatar

    Hi Eli,

    That’s a complex topic!
    “That's because mediator should contain only the logic related to the framework itself?”

    Simply put, if you follow the MVCS pattern as implemented in robotlegs, the Mediator is considered to be a bridge between a View ( UI Component) and the rest of your application (other Mediators, Models, Services, Comands).
    If different Views need to communicate with each other they’ll do it through their Mediators, never directly.
    Mediators listen for events dispatched by their Views and relay them to the rest of the application.
    Mediators listen for events dispatched by a Service, or Model or other Mediators and can access public methods in their Views, either to set some data coming from a Model or Service, or to let the view perform some actions.

    Application<---shared eventDispatcher<---Event<---Mediator<---Event<---View

    Application--->shared eventDispatcher --->Event--->Mediator--->View.API--->view actions

    Ideally, you just register event listeners in your Mediator.onRegister() and have handler methods, which either call a method on the View or re-dispatch an event coming from the View. Nothing more. Ideally!

    This way your Views are loosely coupled and reusable and so will be your Models and Services as well.

    SomeView dispatches a custom event SomeRequestEvent.LOAD_DATA
    SomeMediator re-dispatches the event, which will trigger SomeCommand.
    SomeCommand will call a method on SomeService. accessResources()
    SomeService will either set the result in SomeModel
    or will dispatch an event that will trigger a command, that will set the SomeModel data.
    SomeModel will dispatch an event with the data as a payload, SomeMediator will pick it up and pass the payload to SomeView, which will set the dataProvider of a List or a DataGrid with the payload.
    Selecting an item from the List will dispatch an event, SomeMediator will re-dispatch it, AnotherMediator will pick it up and let AnotherView perform some action depending on the selectedItem.

    I could give you a link to an example, which uses this logic, if the above isn’t clear enough.

    It would take me too long to explain more and I would only repeat what was already said many times and in much better words than mines. I would suggest reading:

    You can, of course, ask more questions about this or any other topic, or if something I said was confusing :)

    In your case the ContextMenu belongs to the View (ThumbsGallery) and also the methods you are now having inside ThumbsGalleryMediator related to the ContextMenu should reside in the ThumbsGallery.
    If you need the rest of your application to react to menuItemSelected you can dispatch a custom event in the view’s handler method:

    protected function menuItemSelected(evt:ContextMenuEvent):void

    dispatchEvent(new SomeContextMenuEvent(SomeContextMenuEvent.ITEM_SELECTED, somePayload);


    ThumbsGalleryMediator.onRegister() registers a listener for it:
    eventMap.mapListener(_thumbsGalleryMainView, SomeContextMenuEvent.ITEM_SELECTED, dispatch);
    and will re-dispatch SomeContextMenuEvent to the rest of your application.

    Let me know what you think:)


  4. 4 Posted by Eli Atlas on 26 Dec, 2011 03:19 PM

    Eli Atlas's Avatar

    Thanks Ondina, this is great explanation :)

  5. Support Staff 5 Posted by Ondina D.F. on 28 Dec, 2011 03:22 PM

    Ondina D.F.'s Avatar

    Thanks, Elli:)
    I'm glad it was helpful.

  6. Ondina D.F. closed this discussion on 28 Dec, 2011 03:22 PM.

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

Keyboard shortcuts


? 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