tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/630-robotlegsRobotlegs: Discussion 2012-08-25T17:10:04Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/179167292012-08-09T04:33:54Z2012-08-09T04:33:55ZRobotlegs<div><p>The mediator onRegister() method runs after the view has landed
on stage. But the view has been in the stage.</p></div>249510573tag:robotlegs.tenderapp.com,2009-10-18:Comment/179167292012-08-15T11:08:21Z2012-08-15T11:08:21ZRobotlegs<div><p>Hello 249510573,</p>
<p>I've had a look at your code.<br>
Possible solutions:</p>
<ul>
<li>
<p>Stray’s <strong>RelaxedEventMap</strong> (<a href=
"https://github.com/Stray/robotlegs-utilities-RelaxedEventMap">https://github.com/Stray/robotlegs-utilities-RelaxedEventMap</a>)</p>
</li>
<li>
<p><strong>Ask for data within your Mediator’s
onRegister()</strong> method: dispatch an event triggering a
command, that calls the Service (after registering an event
listener for the event dispatched by the Model in Mediator’s
onRegister()):</p>
</li>
</ul>
<pre>
<code>
override public function onRegister():void
{
//first add the listener !!
eventMap.mapListener(eventDispatcher, SystemEvent.LOADED_GRIDDATA, whenGridDataLoaded);
//then dispatch the event
dispatch(new SystemEvent(SystemEvent.LOAD_GRIDDATA));
}</code>
</pre>
<ul>
<li>make the service call on
<strong>FlexEvent.APPLICATION_COMPLETE</strong>:</li>
</ul>
<p>In your FirstDemo.mxml add an event listener for
FlexEvent.PREINITIALIZE:<br>
<s:Application …<br>
preinitialize="application_preinitializeHandler(event)" …</p>
<p>Script:</p>
<pre>
<code>
import demo.FirstDemoContext;
import mx.events.FlexEvent;
private var context:FirstDemoContext;
protected function application_preinitializeHandler(event:FlexEvent):void
{
context=new FirstDemoContext(this);
}</code>
</pre>
<p>In your FirstDemoContext:</p>
<pre>
<code>
public function FirstDemoContext(contextView:DisplayObjectContainer=null, autoStartup:Boolean=true)
{
super(contextView, autoStartup);
this.contextView.addEventListener(FlexEvent.APPLICATION_COMPLETE, onApplicationComplete);
}
private function onApplicationComplete(event:FlexEvent):void
{
dispatchEvent(new SystemEvent(SystemEvent.LOAD_GRIDDATA));
}</code>
</pre>
<p>Your DataGridMediator will be able to hear the event and to set
dataGrid.dataProvider.</p>
<p>If you want to get into details about the start up process of a
Flex Application using the Robotlegs framework, you can take a look
at the diagrams (especially pages 58-62) from this document:<br>
<a href=
"http://cloud.github.com/downloads/Ondina/robotlegs-incremental/robotlegs_incremental_slides.pdf">
http://cloud.github.com/downloads/Ondina/robotlegs-incremental/robo...</a><br>
I have to say, that I never had the time to finish it or make it
better, but, hopefully, it will at least give you an idea about the
application’s availability and readiness to consume external
services and respond to user input.</p>
<ul>
<li>The disputed <strong>‘Inject your Model into your
Mediator’</strong>- if you choose this, then inject your
Model against an interface. But, as I can see, you’re
actually injecting DataGridModel into DataGridMediator and at the
same time you’re letting DataGridModel dispatch
SystemEvent.LOADED_GRIDDATA. You could avoid injecting the Model
into the Meditor by sending dataGridModel.getData as the
SystemEvent’s payload. By the way, SystemEvent is not a very
good name for your custom event:)</li>
</ul>
<p>Your custom event would look like this:</p>
<pre>
<code>
public class SystemEvent extends Event
{
public static const LOAD_GRIDDATA:String='loadGridData';
public static const LOADED_GRIDDATA:String='loadedGridData';
private var _someData:ArrayCollection;
public function SystemEvent(type:String, someData:ArrayCollection=null, bubbles:Boolean=false, cancelable:Boolean=false)
{
_someData=someData;
super(type, bubbles, cancelable);
}
public function get someData():ArrayCollection
{
return _someData;
}
override public function clone():Event
{
return new SystemEvent(type, someData, bubbles, cancelable);
}
}</code>
</pre>
<p>In your DataGridModel you’d do this:<br>
dispatch(new SystemEvent(SystemEvent.LOADED_GRIDDATA,
getData));</p>
<p>And in your DataGridMediator. whenGridDataLoaded():<br>
dataGrid.dataProvider = event.someData;</p>
<p>If you want to know more about how the Robotlegs Mediators work,
please read :</p>
<p><a href=
"https://github.com/robotlegs/robotlegs-framework/wiki/best-practices#wiki-automaticmediation">
https://github.com/robotlegs/robotlegs-framework/wiki/best-practice...</a></p>
<p><a href=
"http://knowledge.robotlegs.org/kb/reference-mvcs-implementation/are-view-components-automatically-mediated">
http://knowledge.robotlegs.org/kb/reference-mvcs-implementation/are...</a></p>
<p>and other articles under:<br>
<a href=
"http://knowledge.robotlegs.org/kb">http://knowledge.robotlegs.org/kb</a></p>
<p>Is everything clear now? :)<br>
Ondina</p></div>Ondina D.F.