One mediator for multiple view instances

markus.vieghofer's Avatar

markus.vieghofer

23 Nov, 2012 12:31 PM

Hi,
I have a mxml component call Template.mxml and a corresponding mediator called TemplateMediator. The template object dispatches an event which should be dispatch to the framework by the mediator. As long as I have only one instance of that template everything works fine. The one template dispatches an event and the mediator dispatches the event to the framework.
However, when I have more than one instance of the template the mediator only gets the last dispatched event. My assumption is that it is only possible to wire one instance of a view component with one mediator is that true? If it is true, what would be a workaround? I will need an undefined number of templates so I have to make it work in some way.

kind regards
Markus

  1. Support Staff 1 Posted by Ondina D.F. on 23 Nov, 2012 03:54 PM

    Ondina D.F.'s Avatar

    Hi Markus,

    I’m not quite sure what you want or expect: each view with its own mediator, or one mediator for multiple views?

    If you have something like this:
    mediatorMap.mapView(SomeView, SomeMediator);

    And then you add multiple instances of SomeView to the stage, each instance will have its own Mediator, and the Mediator will respond to the events dispatched by the paired View. You can see that if you give SomeView(on creationcomplete) and SomeMediator(on register) random names and you trace them, or dispatch the name of the view to the mediator..
    If that’s what you want and it doesn’t work, it probably has to do with the way you’re adding/removing the views to/from stage. Maybe some parenting issues, or you’re using states, transitions, or it is a pure as3 project( some bug, views are added twice), or you add/remove views within a loop.
    (See: http://knowledge.robotlegs.org/discussions/problems/515-mediator-do...)

    If that’s the case, and you can’t figure it out by yourself, you either paste the code where you are adding/removing the views, or, better, attach an example that reproduces the issues.

    Ondina

  2. 2 Posted by markus.vieghofe... on 25 Nov, 2012 05:04 PM

    markus.vieghofer's Avatar

    Thanks for your answer, here is what I do:

    in the config I add the mediator to the view this way:

    mediatorMap.map(ITemplate).toMediator(TemplateMediator);

    My view (Template.mxml) implements the ITemplate interface. In the View I dispatch the events like this:

    dispatchEvent(new ContentEvent(ContentEvent.SAVING_COMPLETE));

    in the medaitor I listening for theses events like this:

    addViewListener(ContentEvent.SAVING_COMPLETE, dispatch, ContentEvent);

    As long I only have only one Instance of each view the mediator and the view communicate with each other as it should be. When I add more than one view, it seems that only one view gets an mediator.

    I need to mention here, that I am using Robotlegs 2.0, so it may be possible this is a bug in the framework. Is there any such issue known?

  3. Support Staff 3 Posted by Ondina D.F. on 25 Nov, 2012 06:38 PM

    Ondina D.F.'s Avatar

    Hi Markus,

    Nope there is no bug, as far as I know. It works with rl2, too. Have you injected your view into the mediator like this?

    [Inject] public var view: ITemplate;

    You should do as I said, give mediators and views random names and you’ll see that each view will have its own mediator, and each mediator will listen for its view’s events.

    I think that you’re experiencing something else, namely, you forgot to override clone() in your custom event?? If you had a handler for the event dispatched by the view in the mediator, you could see that the mediator is listening and handling it correctly, but without overriding clone() the event can’t be redispatched on the shared event dispatcher.

    The dispatch in addViewListener(ContentEvent.SAVING_COMPLETE, dispatch, ContentEvent); is actually redispatching the event to the framework.

    http://knowledge.robotlegs.org/kb/application-architecture/why-does...

    Hope that helps.

    Ondina

  4. 4 Posted by markus.vieghofe... on 25 Nov, 2012 07:40 PM

    markus.vieghofer's Avatar

    Hi,
    thanks for your prompt response. Here is the code of the mediator:

    public class TemplateMediator extends Mediator

    {
        override public function initialize():void
        {
            addViewListener(ContentEvent.SAVING_COMPLETE, dispatchContentEvent, ContentEvent);
        }
    
        private function dispatchContentEvent(event:ContentEvent):void
        {
            dispatch(event);
        }
    }
    

    I created the dispatchContentEvent method to check if the mediator get the events from the view. In the event, I override the clone method, so that should not be the problem.

    I just created a private variable in the view called key which is initialized with the UIDUtil.createUID(). In the initialize method of the mediator I trace the key of the template and it seems that each Template has the same key (although the content is not the same). It seems that there could be a problem. I will test it and as soon as I have results I will post it here.

    Until then, thank you for listening and guiding me (hopefully) into the right direction.

  5. Support Staff 5 Posted by Ondina D.F. on 26 Nov, 2012 07:51 AM

    Ondina D.F.'s Avatar

    No problem.

    See if Joel’s example, WheresWindow, can inspire you regarding UIDs for views:

    https://github.com/robotlegs/robotlegs-demos-Bundle/tree/master/Whe...

  6. Support Staff 6 Posted by Ondina D.F. on 26 Nov, 2012 06:02 PM

    Ondina D.F.'s Avatar

    I’ve created a very simple example – see attachment. I’ve spent only half an hour to create it, so you’ll have to excuse the missing parts or the simple layout or anything else :)
    It’s an air app, using rl2, built in FlashBuilder 4.6.
    I’m giving each added view (SomeView) a random name, which is stored in a dictionary in SomeMasterView.

    In a more complex scenario I’d use a Model and a VO to manage the views’ names or ids.

  7. 7 Posted by markus.vieghofe... on 27 Nov, 2012 01:38 PM

    markus.vieghofer's Avatar

    So I found and fixed the error. The problem was, that I assumed, that a mediator is always attached to a view, even when the view is not attached to the stage. In my case, only one of the dynamically added views are attached to the view at a time. The others are kept in memory.

    I solved the issue by creating a class that holds the shared eventdispatcher. I use this class (and therefore the shared evendDispatcher directly) to dispatch the events to the framework and now it works fine.

    Thanks again for the help.

  8. markus.vieghofer closed this discussion on 27 Nov, 2012 01:38 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