Different mediators for same view Class

kamcknig's Avatar

kamcknig

21 Nov, 2016 06:04 PM

How can I create multiple different mediators mediate the same Type of class in different circumstances.

Example, views with components:

FirstView
    FirstComponent
SecondView
    FirstComponent

I want FirstComponent inside of FirstView to be mediated by FirstComponentMediator.
But I want FirstComponent inside of SecondView to be mediated by SecondComponentMediator.

  1. Support Staff 1 Posted by Ondina D.F. on 22 Nov, 2016 11:18 AM

    Ondina D.F.'s Avatar

    mediatorMap.mapMatcher and interfaces are your friends in this case.

    A ViewWithManyMediators would implement an interface, say IDoStuff.

    The mappings would look like this:

    mediatorMap
    .mapMatcher(new TypeMatcher().allOf(IDoStuff, ViewWithManyMediators))
    .toMediator(FirstMediator);
    
    mediatorMap
    .mapMatcher(new TypeMatcher().allOf(IDoStuff, ViewWithManyMediators))
    .toMediator(SecondMediator);
    

    But, FirstMediator and SecondMediator would both mediate each of the 2 instances of the View.

    If you want FirstMediator to mediate only the first instance of the View, and SecondMediator only the second instance of the View, you need to make use of guards:

    mediatorMap
    .mapMatcher(new TypeMatcher().allOf(IDoStuff, ViewWithManyMediators))
    .toMediator(FirstMediator).withGuards(FirstGuard);
    
    mediatorMap
    .mapMatcher(new TypeMatcher().allOf(IDoStuff, ViewWithManyMediators))
    .toMediator(SecondMediator).withGuards(SecondGuard);
    

    As an example, you could approve the mediation of the first instance of ViewWithManyMediators by the FirstMediator only if the view's name is "firstInstance". Similar for the second instance, if the view's name is "secondInstance", SecondMediator would be the one mediating the second instance of the view added to the stage.

    So, ViewWithManyMediators exposes a setter and getter for the viewName.
    When you add the 2 views to the stage, you set their names accordingly, "firstInstance" and "secondInstance".

    A guard would look like this:

    public class FirstGuard
    {
        [Inject] public var view:IDoStuff;
            
        public function approve():Boolean
        {
            return view.viewName=="firstInstance";
        }
    }
    public class SecondGuard
    {
        [Inject] public var view:IDoStuff;
            
        public function approve():Boolean
        {
            return view.viewName=="secondInstance";
        }
    }
    

    IDoStuff should, of course, contain the getter and setter for the viewName, so that the view can implement them.

    When the mediators are about to be created, the guards will make sure that each instance of the view on stage will have different mediators.

    Both mediators are injected with IDoStuff, as are the 2 guards as well.
    [Inject] public var view:IDoStuff;

    The 2 instances of the view can exhibit a common behaviour or different behaviours, dictated by the use of one or more interfaces. Here is a link to an example that shows how to use interfaces to create and mediate behaviours, in case you are interested in the subject:

    http://knowledge.robotlegs.org/discussions/robotlegs-2/12476-rl2-bo...

    The zip file is at the end of my answer.

    About guards:
    https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...

    About mapMatcher:
    https://github.com/robotlegs/robotlegs-framework/tree/master/src/ro...

    I hope that helps.
    Ondina

  2. 2 Posted by kamcknig on 22 Nov, 2016 03:12 PM

    kamcknig's Avatar

    As always, great answer!

  3. Support Staff 3 Posted by Ondina D.F. on 23 Nov, 2016 01:16 PM

    Ondina D.F.'s Avatar

    I'm glad it was helpful :)

  4. Ondina D.F. closed this discussion on 23 Nov, 2016 01:16 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