Different mediators for same view Class
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.
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
Support Staff 1 Posted by Ondina D.F. on 22 Nov, 2016 11:18 AM
mediatorMap.mapMatcher and interfaces are your friends in this case.
A ViewWithManyMediators would implement an interface, say IDoStuff.
The mappings would look like this:
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:
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:
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 Posted by kamcknig on 22 Nov, 2016 03:12 PM
As always, great answer!
Support Staff 3 Posted by Ondina D.F. on 23 Nov, 2016 01:16 PM
I'm glad it was helpful :)
Ondina D.F. closed this discussion on 23 Nov, 2016 01:16 PM.