tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/1345-one-mediator-for-multiple-view-instancesRobotlegs: Discussion 2018-10-18T16:35:44Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/213276842012-11-23T12:31:09Z2012-11-25T16:51:37ZOne mediator for multiple view instances<div><p>Hi,<br>
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.<br>
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.</p>
<p>kind regards<br>
Markus</p></div>markus.vieghofertag:robotlegs.tenderapp.com,2009-10-18:Comment/213276842012-11-23T15:54:17Z2012-11-23T15:54:17ZOne mediator for multiple view instances<div><p>Hi Markus,</p>
<p>I’m not quite sure what you want or expect: each view with
its own mediator, or one mediator for multiple views?</p>
<p>If you have something like this:<br>
mediatorMap.mapView(SomeView, SomeMediator);</p>
<p>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..<br>
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.<br>
(See: <a href=
"http://knowledge.robotlegs.org/discussions/problems/515-mediator-doesnt-seem-to-remove-itself#comment_14540578">
http://knowledge.robotlegs.org/discussions/problems/515-mediator-do...</a>)</p>
<p>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.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/213276842012-11-25T17:04:24Z2012-11-25T17:04:29ZOne mediator for multiple view instances<div><p>Thanks for your answer, here is what I do:</p>
<p>in the config I add the mediator to the view this way:</p>
<p>
<code>mediatorMap.map(ITemplate).toMediator(TemplateMediator);</code></p>
<p>My view (Template.mxml) implements the ITemplate interface. In
the View I dispatch the events like this:</p>
<p><code>dispatchEvent(new
ContentEvent(ContentEvent.SAVING_COMPLETE));</code></p>
<p>in the medaitor I listening for theses events like this:</p>
<p><code>addViewListener(ContentEvent.SAVING_COMPLETE, dispatch,
ContentEvent);</code></p>
<p>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.</p>
<p>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?</p></div>markus.vieghofertag:robotlegs.tenderapp.com,2009-10-18:Comment/213276842012-11-25T18:38:15Z2012-11-25T18:45:17ZOne mediator for multiple view instances<div><p>Hi Markus,</p>
<p>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?</p>
<p>[Inject] public var view: ITemplate;</p>
<p>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.</p>
<p>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.</p>
<p>The <em>dispatch</em> in
addViewListener(ContentEvent.SAVING_COMPLETE, dispatch,
ContentEvent); is actually redispatching the event to the
framework.</p>
<p><a href=
"http://knowledge.robotlegs.org/kb/application-architecture/why-doesnt-my-event-trigger-the-command-it-is-mapped-to">
http://knowledge.robotlegs.org/kb/application-architecture/why-does...</a></p>
<p>Hope that helps.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/213276842012-11-25T19:40:01Z2012-11-25T19:40:02ZOne mediator for multiple view instances<div><p>Hi,<br>
thanks for your prompt response. Here is the code of the
mediator:</p>
<p>public class TemplateMediator extends Mediator</p>
<pre>
<code>{
override public function initialize():void
{
addViewListener(ContentEvent.SAVING_COMPLETE, dispatchContentEvent, ContentEvent);
}
private function dispatchContentEvent(event:ContentEvent):void
{
dispatch(event);
}
}</code>
</pre>
<p>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.</p>
<p>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.</p>
<p>Until then, thank you for listening and guiding me (hopefully)
into the right direction.</p></div>markus.vieghofertag:robotlegs.tenderapp.com,2009-10-18:Comment/213276842012-11-26T07:51:31Z2012-11-26T07:51:31ZOne mediator for multiple view instances<div><p>No problem.</p>
<p>See if Joel’s example, <em>WheresWindow</em>, can inspire
you regarding UIDs for views:</p>
<p><a href=
"https://github.com/robotlegs/robotlegs-demos-Bundle/tree/master/WheresWindow">
https://github.com/robotlegs/robotlegs-demos-Bundle/tree/master/Whe...</a></p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/213276842012-11-26T18:02:34Z2012-11-26T18:02:34ZOne mediator for multiple view instances<div><p>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 :)<br>
It’s an air app, using rl2, built in FlashBuilder 4.6.<br>
I’m giving each added view (SomeView) a random name, which is
stored in a dictionary in SomeMasterView.</p>
<p>In a more complex scenario I’d use a Model and a VO to
manage the views’ names or ids.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/213276842012-11-27T13:38:28Z2012-11-27T13:38:29ZOne mediator for multiple view instances<div><p>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.</p>
<p>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.</p>
<p>Thanks again for the help.</p></div>markus.vieghofer