One mediator for a Parent listening to many children

antoine.amassih's Avatar

antoine.amassih

01 Dec, 2010 01:30 PM via web

Hi i am very new at robotlegs,

Im trying to create a single mediator for a grid where each square in the grid is a different view and has independant events ( such as click ) attached to them.

there are of the order of 150 squares and it seems unreasonable to have a mediator for each one especially since only one signal can be fired at a time, so i am trying to create a parent grid and a mediator attached to that listening to signals for the children, i am trying not to do this using some central singleton which handles the signals and also i would like to leave it independant of the main context.

Thanks

  1. 2 Posted by Abel de Beer on 01 Dec, 2010 03:35 PM

    Abel de Beer's Avatar

    It sounds very logical to create one view/mediator combination for the grid that contains all your 'square views'. Especially if they all fire the same types of events. For example, if all your squares dispatch a SquareEvent.CLICKED event, your grid view could listen for this event and dispatch a framework event (or signal of course) in response.

  2. 3 Posted by antoine.amassih on 01 Dec, 2010 03:52 PM

    antoine.amassih's Avatar

    so how would the Mediator listen for the child event since its mapped to the Parent View.

    mediatorMap.mapView(ParentView, ParentViewMediator);

    Again im new , is it through :

    addContextListener(SquareEvent.CLICKED , handleMessage)

    for example even though the child view is not mapped?

  3. 4 Posted by Abel de Beer on 01 Dec, 2010 05:37 PM

    Abel de Beer's Avatar

    Almost! You should do addViewListener(SquareEvent.CLICKED, square_onClicked);. The event.target property should reference the clicked square.

  4. 5 Posted by antoine.amassih on 01 Dec, 2010 05:45 PM

    antoine.amassih's Avatar

    Will try it out Thanks for the help, Cheers

  5. 6 Posted by antoine.amassih on 05 Dec, 2010 05:40 PM

    antoine.amassih's Avatar

    Abel,

    So i tried what you suggested :

    HexGrid View Contains HexBitmap View

    i dispatch an Event in the HexBitmap View :

    dispatchEvent(new HexEvent(HexEvent.HEX_CLICKED));

    Which works fine then i add this to the context:

    mediatorMap.mapView(HexGrid, HexGridMediator);

    and the mediator :

    `import org.robotlegs.demos.helloflash.controller.HexEvent;

    import org.robotlegs.mvcs.Mediator;
    
    public class HexGridMediator extends Mediator
    {
        [Inject]
        public var view:HexGrid;
    
        public function HexGridMediator()
        {
    
        }
    
        override public function onRegister():void
        {
            addContextListener(HexEvent.HEX_CLICKED, OnSomeHexClicked);
            //eventMap.mapListener(eventDispatcher, HexEvent.HEX_CLICKED, OnSomeHexClicked);
        }
    
        protected function OnSomeHexClicked(e:HexEvent):void
        {
            // Manipulate the view
            trace("This Event Was Received");
        }
    }`
    

    Which for some reason doesnt accept the addViewListener or addContextListener . and of course it doesnt work.

    Suggestions ?

    Thanks

  6. Support Staff 7 Posted by Shaun Smith on 05 Dec, 2010 05:46 PM

    Shaun Smith's Avatar

    Make sure your HexEvent bubbles.

  7. 8 Posted by antoine.amassih on 05 Dec, 2010 06:07 PM

    antoine.amassih's Avatar

    Hey Shaun,

    Thanks but it does isnt it just
    `public function HexEvent(type:String, body:* = null)

        {
            super(type,true,true);
            _body = body;
        }`
    

    it doesnt work, i forgot to mention that im using:

    eventMap.mapListener(eventDispatcher, HexEvent.HEX_CLICKED, OnSomeHexClicked);

    because its not recognizing addViewListener in the mediator.

  8. Support Staff 9 Posted by Shaun Smith on 05 Dec, 2010 06:11 PM

    Shaun Smith's Avatar

    Howdy. You need to override the clone method in your event or it won't bubble properly.

  9. 10 Posted by antoine.amassih on 05 Dec, 2010 06:14 PM

    antoine.amassih's Avatar

    This is the whole Event Class

    ` package org.robotlegs.demos.helloflash.controller
    {

    import flash.events.Event;
    
    public class HexEvent extends Event
    {
        public static const HEX_CLICKED:String = 'HEX_CLICKED';
    
        private var _body:*;
    
        public function HexEvent(type:String, body:* = null)
        {
            super(type,true,true);
            _body = body;
        }
    
        public function get body():*
        {
            return _body;
        }
    
        override public function clone():Event
        {
            return new HexEvent(type, body);
        }
    
    }
    

    }

    `

    its overriden

  10. Support Staff 11 Posted by Shaun Smith on 05 Dec, 2010 06:26 PM

    Shaun Smith's Avatar

    Hi. Looks like you're listening to the shared event dispatcher for those events, which doesn't make much sense: eventMap.mapListener(eventDispatcher, HexEvent.HEX_CLICKED, OnSomeHexClicked);

    Surely you want to be listening to the view?

    eventMap.mapListener(view, HexEvent.HEX_CLICKED, OnSomeHexClicked);

  11. 12 Posted by antoine.amassih on 05 Dec, 2010 06:28 PM

    antoine.amassih's Avatar

    Thanks a bunch for the quick responses, Works

  12. Support Staff 13 Posted by Shaun Smith on 05 Dec, 2010 06:31 PM

    Shaun Smith's Avatar

    It's a pleasure. Glad you're back on track

  13. Shaun Smith closed this discussion on 05 Dec, 2010 06:31 PM.

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