Mediators and multiple instances of the view.

oscar's Avatar

oscar

13 Mar, 2010 07:48 PM

Hi,

I have a view that is added at run time. Most of the times, I have several instances of that view. In my context, I'm mapping the mediator:

 mediatorMap.mapView(MyView, MyViewMediator);

My problem is that when one of the views dispatch an event, all mediator response to it. I want that just the mediator of the instance that dispatched the event is able to listen to it.

I can use ids, to check id the mediators match the id of the event, but it should be a better way to do it.

            [Inject]
        public var myView:MyView;

            private function handleEvent(event:CustomEvent):void{
                  if(myView.viewid == event.viewid){
                        //do something
                   }
            }
  1. 1 Posted by oscar on 13 Mar, 2010 08:15 PM

    oscar's Avatar

    Same question for Commands. If a command is going to dispatch an event that will trigger an action in a mediator of an specific instance of a view, how do I manage to have only that mediator to pick up that event?

    Tks.

  2. Support Staff 2 Posted by Till Schneidere... on 13 Mar, 2010 08:28 PM

    Till Schneidereit's Avatar

    Your mediators shouldn't all get these events. As each of the
    mediators is injected with a reference to the one view it is
    mediating, you should be able to listen to events from that view and
    really only get those, not the events from other views of the same
    type.

    The only simple explanation for that behavior to occur is if your
    views are nested into one another on the display list and your events
    are bubbling.
    If that's the case, you'll have to either change the event dispatching
    to make them non-bubbling, or check the event.target property to
    verify that the event was dispatched by the right view.

    As for events dispatched from commands: Commands shouldn't normally
    act on mediators at all. Mediators should instead listen to events on
    the event bus and react to them. These events might of course be
    dispatched by commands if that makes sense in the context of your
    application.

  3. 3 Posted by oscar on 13 Mar, 2010 10:29 PM

    oscar's Avatar

    Totally right. my views were nested into others views and the events were bubbling.

    Tks.

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