tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/2000-passing-data-between-viewsRobotlegs: Discussion 2014-08-07T12:11:03Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/251488982013-02-22T12:32:43Z2013-02-24T18:34:48ZPassing data between views<div><p>Hi Vitor,</p>
<p>Sorry for the late response.</p>
<blockquote>
<p>I need to do this, because the view doesn't at stage yet, so the
mediator do not catch any event.</p>
</blockquote>
<p>Here, 2 common solutions for robotlegs v1:</p>
<p>A. The mediator of the view created dynamically dispatches an
event requesting the data:</p>
<ol>
<li>
<p>UserListView->selectedItem as the payload of a custom
event->UserListMediator->redispatches custom
event->SomeCommand->passes the payload on to UserModel<br>
(or calls UserService to load user’s details and sets the
results on UserModel)</p>
</li>
<li>
<p>UserDetailsView gets added to the stage.</p>
</li>
<li>
<p>UserDetailsMediator onRegister()<br>
->dispatches custom
event->AnotherCommand->UserModel->getUserDetails()->custom
event-> UserDetailsMediator ->passes the payload on to
UserDetailsView. UserDetailsMediator should register the listener
for the event dispatched by the model <strong>before</strong>
dispatching the event triggering the command in order to be able to
listen to that event!!!</p>
</li>
</ol>
<p>B. Stray’s RelaxedEventMap (<a href=
"https://github.com/Stray/robotlegs-utilities-RelaxedEventMap">https://github.com/Stray/robotlegs-utilities-RelaxedEventMap</a>)
– probably the best solution</p>
<p>Robotlegs v2 offers more possibilities. One of them is the view
processor map extension, which allows injection of properties into
views. [<a href=
"https://github.com/robotlegs/robotlegs-framework/tree/master/src/robotlegs/bender/extensions/viewProcessorMap">https://github.com/robotlegs/robotlegs-framework/tree/master/src/ro...</a>]<br>
I’m used to Robotlegs 1' MVCS implementation where injecting
into views wasn’t a very good idea, so I’m not sure yet
about the best practices regarding this, but if you wanted to, you
could do the following:<br>
Config:</p>
<pre>
<code>mediatorMap = injector.getInstance(IMediatorMap);
viewProcessorMap = injector.getInstance(IViewProcessorMap);
injector.map(SomeVO).asSingleton();
viewProcessorMap.map(SomeView).toInjection();
viewProcessorMap.map(ParentView).toInjection();
mediatorMap.map(SomeView).toMediator(SomeMediator);
mediatorMap.map(ParentView).toMediator(ParentMediator);</code>
</pre>
<p>ParentView</p>
<pre>
<code>[Inject]
public var someVO:SomeVO;
protected function parentList_changeHandler(event:IndexChangeEvent):void
{
someVO.someProperty = event.currentTarget.selectedItem.lastName;
addNewViews();
}
protected function addNewViews():void
{
var someView:SomeView= new SomeView();
addElement(someView);
}</code>
</pre>
<p>SomeView</p>
<pre>
<code>[Inject]
public var someVO:SomeVO;
protected function creationCompleteHandler(event:FlexEvent):void
{
//do something with someVO.someProperty
}</code>
</pre>
<p>Let me know if this helps or not.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/251488982013-02-23T16:01:05Z2013-02-24T10:06:57ZPassing data between views<div><p>Hi Ondina, thank's for your reaply!</p>
<p>I'm using Robotlegs V1, so the first approach sounds like more
appropriate to me.</p>
<p>But, if I have more than one UserDetailsView added to stage,
when the user select an item in a list, all UserDetailsView will be
updated, right? Is there a way to prevent this?</p>
<p>I appreciate your help!</p></div>Vitortag:robotlegs.tenderapp.com,2009-10-18:Comment/251488982013-02-24T18:34:11Z2013-02-24T18:34:11ZPassing data between views<div><p>Hi Vitor,</p>
<p>No problem:)</p>
<blockquote>
<p>But, if I have more than one UserDetailsView added to stage,
when the user select an item in a list, all UserDetailsView will be
updated, right?</p>
</blockquote>
<p>That’s right.</p>
<ul>
<li>
<p>If you added a listener for an event coming from a Service/Model
in the onRegister() of the mediator, you can remove the listener in
the handler method of that event:
removeContextListener(SomeEvent.DATA_LOADED, onDataLoaded,
SomeEvent); and this mediator will be deaf from now on:)</p>
</li>
<li>
<p>Or, in the hadler method you check against a Boolean</p>
<pre>
<code>if(view.needsData)
{
view.onDataLoaded(event.payload);
view.needsData=false;
}</code>
</pre></li>
<li>
<p>Or, you decide which mediator should react to an event based on
Views’ IDs.</p>
</li>
<li>
<p>or other solutions that don’t come to mind right now</p>
</li>
</ul>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/251488982013-02-24T21:13:42Z2013-02-24T21:13:45ZPassing data between views<div><p>Ok, good job!</p>
<p>But, I have one more question...</p>
<p>How can I react to an event based on Views’ IDs?</p>
<p>Best practices to do this?</p></div>Vitortag:robotlegs.tenderapp.com,2009-10-18:Comment/251488982013-02-25T09:45:31Z2013-02-25T09:45:31ZPassing data between views<div><p>When you dispatch your event requesting data you pass the
view’s UID along in the event’s payload.<br>
The event to which the mediator listens carries that UID back to
the mediator.<br>
if (event.payload.viewId==view.id)<br>
{</p>
<p>view.onDataLoaded(event.payload);<br>
}</p>
<p>It depends on your use case and your preferences how you create
and use those ids. You can assign an id for a view when you add it
to the stage, directly or using a factory or you can keep a map
(dictionary) of views’ ids in a model.<br>
Take a look at Joel’s example for inspiration:<br>
<a href=
"https://github.com/robotlegs/robotlegs-demos-Bundle/blob/master/WheresWindow/src/org/robotlegs/demos/whereswindow/model/OpenWindowsProxy.as">
https://github.com/robotlegs/robotlegs-demos-Bundle/blob/master/Whe...</a></p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/251488982013-02-26T13:42:07Z2013-02-26T14:52:46ZPassing data between views<div><p>Ok, this is it.</p>
<p>Thanks Odina for your quick replies!</p>
<p>I appreciate your help!</p></div>Vitortag:robotlegs.tenderapp.com,2009-10-18:Comment/251488982013-02-26T15:14:06Z2013-02-26T15:14:06ZPassing data between views<div><p>My pleasure!</p></div>Ondina D.F.