tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/253-understanding-the-flow-of-automatically-mediated-views-via-added_to_stageRobotlegs: Discussion 2013-04-28T10:27:00Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T10:39:02Z2010-08-19T10:39:02ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>when MainSceneMediator adds TestView its ADDED_TO_STAGE gets bubbled up to the context and then handled via the injector.</p>
<p>not sure what you mean by<br />
but I found it less magical when the mainScene was added directly to the contextView.</p></div>Nikos tag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T11:17:46Z2010-08-19T11:17:46ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>Ok so basically any of the RobotLegs set of actors (Mediators, Commands, Contexts) specifically chooses to bubble the ADDED_TO_STAGE events?</p></div>tacklemccleantag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T11:25:16Z2010-08-19T11:25:16ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>the ADDED_TO_STAGE has the bubble propery set to true, you would have to override the class to prevent this</p></div>Nikos tag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T11:51:35Z2010-08-19T11:51:35ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>@tacklemcclean: I think you are misunderstanding how auto-mediation works in Robotlegs. It has nothing to do with Mediators, Commands or the Context, and everything to do with the MediatorMap. Check it out:</p>
<p><a href="http://github.com/robotlegs/robotlegs-framework/blob/v1.1.2/src/org/robotlegs/base/MediatorMap.as#L233">http://github.com/robotlegs/robotlegs-framework/blob/v1.1.2/src/org...</a></p>
<p>The MediatorMap listens to the contextView for capture-phase, bubbling ADDED_TO_STAGE events. Every single DisplayObject that lands on stage (but inside the contextView) gets looked at to determine whether or not it has an associated Mediator mapping. If it does we create a mediator for it:</p>
<p><a href="http://github.com/robotlegs/robotlegs-framework/blob/v1.1.2/src/org/robotlegs/base/MediatorMap.as#L261">http://github.com/robotlegs/robotlegs-framework/blob/v1.1.2/src/org...</a></p>
<p>What this means is that you can add a deeply-nested DisplayObjectContainer to stage and that action might create any number of mediators - in other words automatic mediation creates mediators for display objects no matter where they land up, and no matter who adds them to the display list (so long as they land somewhere inside the contextView).</p>
<p>The same thing is true for auto-removal: you might remove one display object from the screen and that action might remove any number of mediators.</p>
<p>I'm not sure that I'm doing a good job of explaining this. Let me know if that makes sense.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T12:14:34Z2010-08-19T12:14:34ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>I have a hard time understanding the concept even when looking at the actual class.</p>
<p>The ADDED_TO_STAGE Event has bubbles set to false by default according to AS3 reference, so I'm trying to figure out who what and where bubbles is set to true instead.</p>
<p>Probably right around where you say "Every single DisplayObject that lands on stage gets looked at". Who looks at it? The MediatorMap? Or the current contextView?</p></div>tacklemccleantag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T12:40:14Z2010-08-19T12:40:14ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>the mediator map</p>
<p>protected override function addListeners():void<br />
</p>
<pre><code> {
if (contextView && enabled && _active)
{
contextView.addEventListener(Event.ADDED_TO_STAGE, onViewAdded, useCapture, 0, true);
contextView.addEventListener(Event.REMOVED_FROM_STAGE, onViewRemoved, useCapture, 0, true);
}
}</code></pre></div>Nikos tag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T12:51:38Z2010-08-19T12:51:38ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>the contextView wont listen to it by default, but if you mediate the contexView you can listen to any event from that view by using</p>
<p>eventMap.mapListener</p></div>Nikos tag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T13:03:43Z2010-08-19T13:03:43ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>@nikos: that's not quite right.</p>
<p>Ok cool, let's look at it again:</p>
<pre>
contextView.addEventListener(Event.ADDED_TO_STAGE, onViewAdded, useCapture, 0, true);
</pre>
<p>see <a href="http://github.com/robotlegs/robotlegs-framework/blob/v1.1.2/src/org/robotlegs/base/MediatorMap.as#L233">L233</a></p>
<p>The important thing here is the third parameter: <code>useCapture</code> which is set to true. Even though ADDED_TO_STAGE events do not bubble in the "bubbling phase" (as documented) they <em>DO</em> bubble in the "capture phase". As far as I know, this is the only place where that happens in the Flash Player. So, essentially, every display object that lands on stage, inside the contextView, will be processed by the onViewAdded() handler.</p>
<p>Cool, let's look at that handler:</p>
<pre>
protected override function onViewAdded(e:Event):void
{
if (mediatorsMarkedForRemoval[e.target])
{
delete mediatorsMarkedForRemoval[e.target];
return;
}
var config:MappingConfig = mappingConfigByViewClassName[getQualifiedClassName(e.target)];
if (config && config.autoCreate)
{
createMediator(e.target);
}
}
</pre>
<p>see <a href="http://github.com/robotlegs/robotlegs-framework/blob/v1.1.2/src/org/robotlegs/base/MediatorMap.as#L253">L253</a></p>
<p>The first block of code deals with the re-parenting of view components and is not relevant to this discussion.</p>
<p>This line is more important:</p>
<pre>
var config:MappingConfig = mappingConfigByViewClassName[getQualifiedClassName(e.target)];
</pre>
<p><code>e.target</code> is the view component being processed. We grab its Class Name, and use that as a key to the <code>mappingConfigByViewClassName</code> dictionary.</p>
<p>If the view component has been mapped for mediation (see: <a href="http://github.com/robotlegs/robotlegs-framework/blob/v1.1.2/src/org/robotlegs/base/MediatorMap.as#L116">L116</a>) the dictionary will give us back a config object.</p>
<p>If we get a config object back, and the config specifies auto-mediation, we create a mediator for the view component:</p>
<pre>
if (config && config.autoCreate)
{
createMediator(e.target);
}
</pre>
<p>see <a href="http://github.com/robotlegs/robotlegs-framework/blob/v1.1.2/src/org/robotlegs/base/MediatorMap.as#L261">L261</a></p>
<p>Easy-peesy-lemon-squeezy!</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T13:12:28Z2010-08-19T13:12:28ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>Cool now I get it!<br />
It all bogged down to the useCapture thingie, so this was essentially me misunderstanding Flash itself rather than the RobotLegs architecture.</p>
<p>I found this line interesting, quoting from the AS3 reference regarding useCapture in addEventListener:<br />
<code>To listen for the event in all three phases, call addEventListener twice, once with useCapture set to true, then again with useCapture set to false.</code></p>
<p>Thanks for all the great help guys, great community!</p></div>tacklemccleantag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T13:20:13Z2010-08-19T13:20:13ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>Thanks for all the great help guys, great community!</p>
<p>yeah, welcome to the fun side</p></div>Nikos tag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-19T13:23:45Z2010-08-19T13:23:45ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>lol i quoted</p>
<p>contextView.addEventListener(Event.ADDED_TO_STAGE, onViewAdded, useCapture, 0, true);</p>
<p>and still got confused, and I'm supposed to be making screencasts on this :)</p></div>Nikos tag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-20T08:54:05Z2010-08-20T08:54:05ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>ah finally get it, I never knew about that capture phase until this morning :)</p></div>Nikos tag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-20T08:55:34Z2010-08-20T08:55:34ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>The capture phase basically allows you to control many children's event stuff from top levels</p></div>Nikos tag:robotlegs.tenderapp.com,2009-10-18:Comment/26291932010-08-21T14:51:31Z2010-08-21T14:51:31ZUnderstanding the flow of automatically mediated views via ADDED_TO_STAGE<div><p>@Nikos: event handing is pretty important stuff when it comes to AS3, so make sure you understand how event propagation and the 3 phases work:</p>
<p><a href="http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html">http://www.adobe.com/devnet/actionscript/articles/event_handling_as...</a></p></div>Shaun Smith