tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/178-where-to-instantiate-view-components-at-runtimeRobotlegs: Discussion 2018-10-18T16:35:12Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/17899422010-05-27T08:52:16Z2010-05-27T08:52:16ZWhere to instantiate view components at runtime.<div><p>Rather than send an event for the view mediator, you could
expose a public function on contextView that just expects a
DisplayObject and run it from this command?</p>
<p>contextView.requestAddToStage(comp);</p>
<p>where contextView has:</p>
<p>public function
requestAddToStage(displayObject:DisplayObject):void...</p>
<p>Or - if the contextView really does just add the components then
have a different event that is more about what the contextView
needs:</p>
<p>ContextViewEvent.REQUEST_ADD_TO_STAGE or something. Again, the
payload only needs to be a DisplayObject.</p>
<p>If the contextView doesn't care that it's a component, then
that's possibly some coupling you can lose.</p>
<p>I tend not to worry so much about mediator/event coupling though
- as long as the actual view isn't importing the application
specific events then the Mediator is doing its job pretty well.</p>
<p>You really need to consider which is best within the wider scope
of your application - is the ComponentCreationEvent required
because other parts are picking it up? Or is it really just a cue
for the main view to add some stuff?</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/17899422010-05-27T12:23:53Z2010-05-27T12:29:55ZWhere to instantiate view components at runtime.<div><p>Stray,</p>
<blockquote>
<p>public function
requestAddToStage(displayObject:DisplayObject):void...</p>
</blockquote>
<p>Makes sense.</p>
<blockquote>
<p>You really need to consider which is best within the wider scope
of your application is the ComponentCreationEvent required because
other parts are picking it up? Or is it really just a cue for the
main view to add some stuff?</p>
</blockquote>
<p>No, at the moment it is just an event with the sole purpose for
the <code>AppMediator</code> to pick-up and add the payload
(IViewComponent) to the <code>ContextView</code>. So it is a cue,
and so I guess the method <code>requestAddToStage</code> as you
suggest would probably be better?</p>
<p>Not quite sure If I follow you here. Do you mean as long as a
DisplayObject (mxml) is not catching some system events and react
to that?<br></p>
<blockquote>
<p>I tend not to worry so much about mediator/event coupling though
- as long as the actual view isn't importing the application
specific events then the Mediator is doing its job pretty well.</p>
</blockquote>
<p>Just a last question. The components are actually added not to
the contextView, but to a child DisplayObject
<code>ComponentListingView</code> of the context view. Here is the
rule.<br></p>
<pre>
mediatorMap.mapView( ComponentListingView , ComponentListMediator , ComponentListingView );
</pre>
<p>How do I then get access to the ComponentListMediator from a
Command? I tried this<br></p>
<pre>
mediatorMap.retrieveMediator( ComponentListingView )
</pre>
but that returns null
<p>Thanks.</p>
<p>Jiri</p></div>jiritag:robotlegs.tenderapp.com,2009-10-18:Comment/17899422010-05-27T12:40:11Z2010-05-27T12:40:11ZWhere to instantiate view components at runtime.<div><blockquote>
<p>Not quite sure If I follow you here. Do you mean as long as a
DisplayObject (mxml) is not catching some system events and react
to that?</p>
<blockquote>
<p>I tend not to worry so much about mediator/event coupling though
- as long as the actual view isn't importing the application
specific events then the Mediator is doing its job pretty well.</p>
</blockquote>
</blockquote>
<p>I meant that even if the Mediator has knowledge of the
ComponentCreationEvent, as long as the view is just receiving a
display object (or component) then the mediator is doing its job of
decoupling the view from the framework.</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/17899422010-05-27T12:45:08Z2010-05-27T13:35:47ZWhere to instantiate view components at runtime.<div><p>Instead of listening for the
<code>ComponentCreationEvent.CREATED</code> in the AppMediator,
listen for that event in the ComponentListMediator.</p></div>Jonitag:robotlegs.tenderapp.com,2009-10-18:Comment/17899422010-05-27T12:53:17Z2010-05-27T12:53:17ZWhere to instantiate view components at runtime.<div><p>Joni,</p>
<p>thanks for the reply. I already have that.<br></p>
<pre>
public class ComponentListMediator extends Mediator
{
[Inject]
public var view:ComponentListingView;
override public function onRegister():void{
this.eventMap.mapListener( this.eventDispatcher , ComponentCreationEvent.CREATED , onComponentCreated, ComponentCreationEvent);
}
private function onComponentCreated(e:ComponentCreationEvent):void{
this.view.addChild( e.component );
}
}
</pre>
<p>I wanted to actually not send the DisplayObject as a payload
with an event. I was looking for something more in the lines of
what Stray was suggesting. No I am getting confused, and should
maybe leave things the way they are.<br>
Adding a public method to the
<code>ComponentListMediator</code>:<br></p>
<pre>
public function requestAddToStage(component:DisplayObject):void{
this.view.addChild( component );
}
</pre>
<p>would mean I would need access to the
<code>ComponentListMediator</code> from the
<code>PrepViewsCommand</code> which I understood is difficult to
achieve.</p></div>jiritag:robotlegs.tenderapp.com,2009-10-18:Comment/17899422010-05-27T13:04:27Z2010-05-27T13:35:47ZWhere to instantiate view components at runtime.<div><p>That's right.<br>
As I understand it, you should never access a Mediator that way.
Instead, you should send an Event as you already are doing.</p>
<p>I've come across the same situation before and I resolved it by
sending an Event requesting the AppMediator (or any other mediator,
it's the same) to call addChild() on the corresponding
viewComponent. Everything worked ok, so I don't see any major
problem with this approach.</p></div>Jonitag:robotlegs.tenderapp.com,2009-10-18:Comment/17899422010-05-27T13:17:15Z2010-05-27T13:17:15ZWhere to instantiate view components at runtime.<div><p>Ok, that is clear then :)</p></div>jiritag:robotlegs.tenderapp.com,2009-10-18:Comment/17899422010-05-27T13:27:23Z2010-05-27T13:27:23ZWhere to instantiate view components at runtime.<div><p>Hi Jiri - I wasn't suggesting adding a public method to the
mediator!</p>
<p>You would add it to the view itself, which, if it's the
contextView, is already available in your command where you create
the event.</p>
<p>So - if it's just adding a child that you're doing, then you
could just do</p>
<p>contextView.addChild(comp)</p>
<p>Your view wouldn't have to have any knowledge at all of the
component classes or the event.</p>
<p>But yes, mediator in / out is via events (or signals) only.</p></div>Stray