tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/344-can-a-child-object-be-mediated-seperately-from-its-mediated-parent-objectRobotlegs: Discussion 2018-10-18T16:35:19Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/37164982010-11-10T00:26:28Z2010-11-10T00:26:28ZCan a child object be mediated seperately from it's mediated parent object?<div><p>Yes, this is possible. Unfortunately, I am not familiar with Flex, but I hope this AS3 example gives you enough information to figure out the Flex routine. This is what you could do:</p>
<p>Say, your popup window is called MyWindowView which is mapped to a Mediator called MyWindowMediator using mapView(). Your MyWindowView for example contains a child called 'temperatureTool' (for example of type HBox) that you give its own Mediator. The first step is to register the new Mediator:</p>
<pre><code>class MyWindowMediator extends Mediator
{
[Inject] myWindowView:MyWindowView;
override public function onRegister():void
{
// Note: make sure myWindowView.temperatureTool has been instantiated already
mediatorMap.registerMediator(myWindowView.temperatureTool, new TemperatureToolMediator());
}
}</code></pre>
<p>I think this code speaks for itself. Next is the TemperatureToolMediator:</p>
<pre><code>class TemperatureToolMediator extends Mediator
{
private var temperatureTool:HBox;
override public function onRegister():void
{
/**
* Note: the actual view component can't be injected using [Inject],
* because it hasn't been mapped in the injector. Fortunately, the
* registerMediator() method made sure the Mediator's viewComponent
* property was set. For convenience, the viewComponent (of type
* Object) can be casted to its actual type:
*/
temperatureTool = HBox(viewComponent);
}
}</code></pre>
<p>An alternative is to create a custom TemperatureTool class which extends HBox so you can just do <code>mediatorMap.mapView(TemperatureTool, TemperatureToolMediator);</code>. But I never experimented with the first option, so I thought it was interesting to mention.</p>
<p>If anyone with more experience with the first option cares to contribute, I would be very much interested! :)</p>
<p>Hope I've been of help.</p></div>Abel de Beertag:robotlegs.tenderapp.com,2009-10-18:Comment/37164982010-11-10T19:28:42Z2010-11-10T19:28:45ZCan a child object be mediated seperately from it's mediated parent object?<div><p>I should have been more clear. The parent object is being instantiated by PopupManager. The child does Extend HBox and is added as a child to the parent.</p>
<p>I'm writing is pure AS3 so the code you've given is very clear. Here's my situation:</p>
<p>in the MAIN Context for the application, I map the mediator thusly:</p>
<p>mediatorMap.mapView(TemperatureConversionTool, TemperatureConversionToolMediator);</p>
<p>Within the onRegister() function of the Parent Mediator instantiation (forecasterToolsWindow), I have the following code:</p>
<p>var temperatureConversionTool:TemperatureConversionTool = new TemperatureConversionTool();</p>
<p>forecasterToolsWindow.addChild(temperatureConversionTool);</p>
<p>This results in the CHILD mediator's onRegister never to be called.</p>
<p>If I remove the mapView from the MAIN Context (or if I do not) and add this after the above code...</p>
<p>mediatorMap.registerMediator(temperatureConversionTool, new TemperatureConversionToolMediator());</p>
<p>when temperatureConversionToolMediator.onRegister is invoked, temperatureConversionTool == null and I cannot for the life of me figure out why.</p>
<p>Thanks for your consideration...</p></div>david.goretag:robotlegs.tenderapp.com,2009-10-18:Comment/37164982010-11-15T20:29:12Z2010-11-15T20:29:14ZCan a child object be mediated seperately from it's mediated parent object?<div><p>The answer is to create a Mediator using the mediatorMap.createMediator command.</p>
<p>var temperatureConversionToolMediator:TemperatureConversionToolMediator = mediatorMap.createMediator(temperatureConversionTool) as TemperatureConversionToolMediator;</p></div>david.gore